diff --git a/PROTOCOL.TXT b/PROTOCOL.TXT index 3d574104..e97f5c7f 100644 --- a/PROTOCOL.TXT +++ b/PROTOCOL.TXT @@ -331,7 +331,8 @@ occured. 0 | protocol version = 2 1-2 | same token as the PULL_RESP packet to acknowledge 3 | TX_ACK identifier 0x05 - 4-end | [optional] JSON object, starting with {, ending with }, see section 6 + 4-11 | Gateway unique identifier (MAC address) + 12-end | [optional] JSON object, starting with {, ending with }, see section 6 6. Downstream JSON data structure ---------------------------------- diff --git a/VERSION b/VERSION index 4a36342f..fd2a0186 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -3.0.0 +3.1.0 diff --git a/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.basic b/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.basic index a9758615..51f0f5a1 100644 --- a/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.basic +++ b/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.basic @@ -4,12 +4,14 @@ "clksrc": 1, /* radio_1 provides clock to concentrator */ "lbt_cfg": { "enable": false, - "rssi_target": 160, /* rssi in dBm = -lbt_rssi_target/2 */ - "nb_channel": 1, - "start_freq": 869525000, - "scan_time_us": 5000, - "tx_delay_1ch_us": 4000000, - "tx_delay_2ch_us": 4000000 + "rssi_target": -80, /* dBm */ + "chan_cfg":[ /* 8 channels maximum */ + { "freq_hz": 867100000, "scan_time_us": 128 }, + { "freq_hz": 867300000, "scan_time_us": 5000 }, + { "freq_hz": 867500000, "scan_time_us": 128 }, + { "freq_hz": 869525000, "scan_time_us": 128 } + ], + "sx127x_rssi_offset": -4 /* dB */ }, "antenna_gain": 0, /* antenna gain, in dBi */ "radio_0": { @@ -18,6 +20,7 @@ "freq": 867500000, "rssi_offset": -165.0, "tx_enable": true, + "tx_notch_freq": 129000, /* [126..250] KHz */ "tx_freq_min": 863000000, "tx_freq_max": 870000000 }, diff --git a/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.beacon b/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.beacon index 839675fb..1813da50 100644 --- a/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.beacon +++ b/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.beacon @@ -4,12 +4,14 @@ "clksrc": 1, /* radio_1 provides clock to concentrator */ "lbt_cfg": { "enable": false, - "rssi_target": 160, /* rssi in dBm = -lbt_rssi_target/2 */ - "nb_channel": 1, - "start_freq": 869525000, - "scan_time_us": 5000, - "tx_delay_1ch_us": 4000000, - "tx_delay_2ch_us": 4000000 + "rssi_target": -80, /* dBm */ + "chan_cfg":[ /* 8 channels maximum */ + { "freq_hz": 867100000, "scan_time_us": 128 }, + { "freq_hz": 867300000, "scan_time_us": 5000 }, + { "freq_hz": 867500000, "scan_time_us": 128 }, + { "freq_hz": 869525000, "scan_time_us": 128 } + ], + "sx127x_rssi_offset": -4 /* dB */ }, "antenna_gain": 0, /* antenna gain, in dBi */ "radio_0": { @@ -18,6 +20,7 @@ "freq": 867500000, "rssi_offset": -165.0, "tx_enable": true, + "tx_notch_freq": 129000, /* [126..250] KHz */ "tx_freq_min": 863000000, "tx_freq_max": 870000000 }, diff --git a/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.gps b/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.gps index 57596194..f25bb7ad 100644 --- a/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.gps +++ b/lora_pkt_fwd/cfg/global_conf.json.PCB_E336.EU868.gps @@ -4,12 +4,14 @@ "clksrc": 1, /* radio_1 provides clock to concentrator */ "lbt_cfg": { "enable": false, - "rssi_target": 160, /* rssi in dBm = -lbt_rssi_target/2 */ - "nb_channel": 1, - "start_freq": 869525000, - "scan_time_us": 5000, - "tx_delay_1ch_us": 4000000, - "tx_delay_2ch_us": 4000000 + "rssi_target": -80, /* dBm */ + "chan_cfg":[ /* 8 channels maximum */ + { "freq_hz": 867100000, "scan_time_us": 128 }, + { "freq_hz": 867300000, "scan_time_us": 5000 }, + { "freq_hz": 867500000, "scan_time_us": 128 }, + { "freq_hz": 869525000, "scan_time_us": 128 } + ], + "sx127x_rssi_offset": -4 /* dB */ }, "antenna_gain": 0, /* antenna gain, in dBi */ "radio_0": { @@ -18,6 +20,7 @@ "freq": 867500000, "rssi_offset": -165.0, "tx_enable": true, + "tx_notch_freq": 129000, /* [126..250] KHz */ "tx_freq_min": 863000000, "tx_freq_max": 870000000 }, diff --git a/lora_pkt_fwd/global_conf.json b/lora_pkt_fwd/global_conf.json index 589c419c..2d0948cb 100644 --- a/lora_pkt_fwd/global_conf.json +++ b/lora_pkt_fwd/global_conf.json @@ -4,12 +4,14 @@ "clksrc": 1, /* radio_1 provides clock to concentrator */ "lbt_cfg": { "enable": false, - "rssi_target": 160, /* rssi in dBm = -lbt_rssi_target/2 */ - "nb_channel": 1, - "start_freq": 869525000, - "scan_time_us": 5000, - "tx_delay_1ch_us": 4000000, - "tx_delay_2ch_us": 4000000 + "rssi_target": -80, /* dBm */ + "chan_cfg":[ /* 8 channels maximum */ + { "freq_hz": 867100000, "scan_time_us": 128 }, + { "freq_hz": 867300000, "scan_time_us": 5000 }, + { "freq_hz": 867500000, "scan_time_us": 128 }, + { "freq_hz": 869525000, "scan_time_us": 128 } + ], + "sx127x_rssi_offset": -4 /* dB */ }, "antenna_gain": 0, /* antenna gain, in dBi */ "radio_0": { @@ -18,6 +20,7 @@ "freq": 867500000, "rssi_offset": -166.0, "tx_enable": true, + "tx_notch_freq": 129000, /* [126..250] KHz */ "tx_freq_min": 863000000, "tx_freq_max": 870000000 }, diff --git a/lora_pkt_fwd/inc/parson.h b/lora_pkt_fwd/inc/parson.h index fd29e9b4..2669a18c 100644 --- a/lora_pkt_fwd/inc/parson.h +++ b/lora_pkt_fwd/inc/parson.h @@ -1,17 +1,17 @@ /* Parson ( http://kgabis.github.com/parson/ ) - Copyright (C) 2013 Krzysztof Gabis - + Copyright (c) 2012 - 2016 Krzysztof Gabis + Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - + The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE @@ -27,49 +27,91 @@ #ifdef __cplusplus extern "C" { -#endif - -#include /* size_t */ +#endif + +#include /* size_t */ -#define PARSON_VERSION 20131130 - /* Types and enums */ typedef struct json_object_t JSON_Object; typedef struct json_array_t JSON_Array; typedef struct json_value_t JSON_Value; -typedef enum json_value_type { - JSONError = 0, +enum json_value_type { + JSONError = -1, JSONNull = 1, JSONString = 2, JSONNumber = 3, JSONObject = 4, JSONArray = 5, JSONBoolean = 6 -} JSON_Value_Type; +}; +typedef int JSON_Value_Type; + +enum json_result_t { + JSONSuccess = 0, + JSONFailure = -1 +}; +typedef int JSON_Status; + +typedef void * (*JSON_Malloc_Function)(size_t); +typedef void (*JSON_Free_Function)(void *); + +/* Call only once, before calling any other function from parson API. If not called, malloc and free + from stdlib will be used for all allocations */ +void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun); - /* Parses first JSON value in a file, returns NULL in case of error */ -JSON_Value * json_parse_file(const char *filename); +JSON_Value * json_parse_file(const char *filename); /* Parses first JSON value in a file and ignores comments (/ * * / and //), returns NULL in case of error */ -JSON_Value * json_parse_file_with_comments(const char *filename); - +JSON_Value * json_parse_file_with_comments(const char *filename); + /* Parses first JSON value in a string, returns NULL in case of error */ -JSON_Value * json_parse_string(const char *string); +JSON_Value * json_parse_string(const char *string); /* Parses first JSON value in a string and ignores comments (/ * * / and //), returns NULL in case of error */ -JSON_Value * json_parse_string_with_comments(const char *string); - -/* JSON Object */ +JSON_Value * json_parse_string_with_comments(const char *string); + +/* Serialization */ +size_t json_serialization_size(const JSON_Value *value); /* returns 0 on fail */ +JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); +JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename); +char * json_serialize_to_string(const JSON_Value *value); + +/* Pretty serialization */ +size_t json_serialization_size_pretty(const JSON_Value *value); /* returns 0 on fail */ +JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes); +JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename); +char * json_serialize_to_string_pretty(const JSON_Value *value); + +void json_free_serialized_string(char *string); /* frees string from json_serialize_to_string and json_serialize_to_string_pretty */ + +/* Comparing */ +int json_value_equals(const JSON_Value *a, const JSON_Value *b); + +/* Validation + This is *NOT* JSON Schema. It validates json by checking if object have identically + named fields with matching types. + For example schema {"name":"", "age":0} will validate + {"name":"Joe", "age":25} and {"name":"Joe", "age":25, "gender":"m"}, + but not {"name":"Joe"} or {"name":"Joe", "age":"Cucumber"}. + In case of arrays, only first value in schema is checked against all values in tested array. + Empty objects ({}) validate all objects, empty arrays ([]) validate all arrays, + null validates values of every type. + */ +JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value); + +/* + * JSON Object + */ JSON_Value * json_object_get_value (const JSON_Object *object, const char *name); const char * json_object_get_string (const JSON_Object *object, const char *name); JSON_Object * json_object_get_object (const JSON_Object *object, const char *name); JSON_Array * json_object_get_array (const JSON_Object *object, const char *name); -double json_object_get_number (const JSON_Object *object, const char *name); -int json_object_get_boolean(const JSON_Object *object, const char *name); +double json_object_get_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ +int json_object_get_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ /* dotget functions enable addressing values with dot notation in nested objects, just like in structs or c++/java/c# objects (e.g. objectA.objectB.value). @@ -79,31 +121,100 @@ JSON_Value * json_object_dotget_value (const JSON_Object *object, const char * const char * json_object_dotget_string (const JSON_Object *object, const char *name); JSON_Object * json_object_dotget_object (const JSON_Object *object, const char *name); JSON_Array * json_object_dotget_array (const JSON_Object *object, const char *name); -double json_object_dotget_number (const JSON_Object *object, const char *name); -int json_object_dotget_boolean(const JSON_Object *object, const char *name); +double json_object_dotget_number (const JSON_Object *object, const char *name); /* returns 0 on fail */ +int json_object_dotget_boolean(const JSON_Object *object, const char *name); /* returns -1 on fail */ /* Functions to get available names */ size_t json_object_get_count(const JSON_Object *object); const char * json_object_get_name (const JSON_Object *object, size_t index); - -/* JSON Array */ + +/* Creates new name-value pair or frees and replaces old value with a new one. + * json_object_set_value does not copy passed value so it shouldn't be freed afterwards. */ +JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value); +JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string); +JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number); +JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean); +JSON_Status json_object_set_null(JSON_Object *object, const char *name); + +/* Works like dotget functions, but creates whole hierarchy if necessary. + * json_object_dotset_value does not copy passed value so it shouldn't be freed afterwards. */ +JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value); +JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string); +JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number); +JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean); +JSON_Status json_object_dotset_null(JSON_Object *object, const char *name); + +/* Frees and removes name-value pair */ +JSON_Status json_object_remove(JSON_Object *object, const char *name); + +/* Works like dotget function, but removes name-value pair only on exact match. */ +JSON_Status json_object_dotremove(JSON_Object *object, const char *key); + +/* Removes all name-value pairs in object */ +JSON_Status json_object_clear(JSON_Object *object); + +/* + *JSON Array + */ JSON_Value * json_array_get_value (const JSON_Array *array, size_t index); const char * json_array_get_string (const JSON_Array *array, size_t index); JSON_Object * json_array_get_object (const JSON_Array *array, size_t index); JSON_Array * json_array_get_array (const JSON_Array *array, size_t index); -double json_array_get_number (const JSON_Array *array, size_t index); -int json_array_get_boolean(const JSON_Array *array, size_t index); +double json_array_get_number (const JSON_Array *array, size_t index); /* returns 0 on fail */ +int json_array_get_boolean(const JSON_Array *array, size_t index); /* returns -1 on fail */ size_t json_array_get_count (const JSON_Array *array); -/* JSON Value */ +/* Frees and removes value at given index, does nothing and returns JSONFailure if index doesn't exist. + * Order of values in array may change during execution. */ +JSON_Status json_array_remove(JSON_Array *array, size_t i); + +/* Frees and removes from array value at given index and replaces it with given one. + * Does nothing and returns JSONFailure if index doesn't exist. + * json_array_replace_value does not copy passed value so it shouldn't be freed afterwards. */ +JSON_Status json_array_replace_value(JSON_Array *array, size_t i, JSON_Value *value); +JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string); +JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number); +JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean); +JSON_Status json_array_replace_null(JSON_Array *array, size_t i); + +/* Frees and removes all values from array */ +JSON_Status json_array_clear(JSON_Array *array); + +/* Appends new value at the end of array. + * json_array_append_value does not copy passed value so it shouldn't be freed afterwards. */ +JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value); +JSON_Status json_array_append_string(JSON_Array *array, const char *string); +JSON_Status json_array_append_number(JSON_Array *array, double number); +JSON_Status json_array_append_boolean(JSON_Array *array, int boolean); +JSON_Status json_array_append_null(JSON_Array *array); + +/* + *JSON Value + */ +JSON_Value * json_value_init_object (void); +JSON_Value * json_value_init_array (void); +JSON_Value * json_value_init_string (const char *string); /* copies passed string */ +JSON_Value * json_value_init_number (double number); +JSON_Value * json_value_init_boolean(int boolean); +JSON_Value * json_value_init_null (void); +JSON_Value * json_value_deep_copy (const JSON_Value *value); +void json_value_free (JSON_Value *value); + JSON_Value_Type json_value_get_type (const JSON_Value *value); JSON_Object * json_value_get_object (const JSON_Value *value); JSON_Array * json_value_get_array (const JSON_Value *value); const char * json_value_get_string (const JSON_Value *value); double json_value_get_number (const JSON_Value *value); int json_value_get_boolean(const JSON_Value *value); -void json_value_free (JSON_Value *value); - + +/* Same as above, but shorter */ +JSON_Value_Type json_type (const JSON_Value *value); +JSON_Object * json_object (const JSON_Value *value); +JSON_Array * json_array (const JSON_Value *value); +const char * json_string (const JSON_Value *value); +double json_number (const JSON_Value *value); +int json_boolean(const JSON_Value *value); + #ifdef __cplusplus } #endif diff --git a/lora_pkt_fwd/readme.md b/lora_pkt_fwd/readme.md index 52296146..d9341542 100644 --- a/lora_pkt_fwd/readme.md +++ b/lora_pkt_fwd/readme.md @@ -194,9 +194,8 @@ configured in 2 different ways: - Real time mode: when GPS is disabled, the value read in sample register is the actual concentrator counter value. - PPS mode: when GPS is enabled, the value read in sample register is the - value -that the concentrator counter had when last GPS’s PPS occurred. So this changes -every second only. + value that the concentrator counter had when last GPS’s PPS occurred. So + this changes every second only. As in our case GPS is enabled (LGW_GPS_EN==1), we need to have a way to get the actual concentrator current time, at any time. For this, a new thread has been added to the packet forwarder (thread_timersync) diff --git a/lora_pkt_fwd/src/jitqueue.c b/lora_pkt_fwd/src/jitqueue.c index 50c40389..dbde8d28 100644 --- a/lora_pkt_fwd/src/jitqueue.c +++ b/lora_pkt_fwd/src/jitqueue.c @@ -49,69 +49,6 @@ static pthread_mutex_t mx_jit_queue = PTHREAD_MUTEX_INITIALIZER; /* control acce /* -------------------------------------------------------------------------- */ /* --- PRIVATE FUNCTIONS DEFINITION ----------------------------------------- */ -static uint32_t time_on_air(struct lgw_pkt_tx_s *packet, bool isBeacon) { - uint8_t SF, H, DE; - uint16_t BW; - uint32_t payloadSymbNb, Tpacket; - double Tsym, Tpreamble, Tpayload; - - switch (packet->bandwidth) { - case BW_125KHZ: - BW = 125; - break; - case BW_250KHZ: - BW = 250; - break; - case BW_500KHZ: - BW = 500; - break; - default: - MSG("ERROR: Cannot compute time on air for this packet, unsupported bandwidth (%u)\n", packet->bandwidth); - return 0; - } - - switch (packet->datarate) { - case DR_LORA_SF7: - SF = 7; - break; - case DR_LORA_SF8: - SF = 8; - break; - case DR_LORA_SF9: - SF = 9; - break; - case DR_LORA_SF10: - SF = 10; - break; - case DR_LORA_SF11: - SF = 11; - break; - case DR_LORA_SF12: - SF = 12; - break; - default: - MSG("ERROR: Cannot compute time on air for this packet, unsupported datarate (%u)\n", packet->datarate); - return 0; - } - - /* Duration of 1 symbol */ - Tsym = pow(2, SF) / BW; - - /* Duration of preamble */ - Tpreamble = (8 + 4.25) * Tsym; /* 8 programmed symbols in preamble */ - - /* Duration of payload */ - H = (isBeacon==false)?0:1; /* header is always enabled, except for beacons */ - DE = (SF >= 11)?1:0; /* Low datarate optimization enabled for SF11 and SF12 */ - - payloadSymbNb = 8 + (ceil((double)(8*packet->size - 4*SF + 28 + 16 - 20*H) / (double)(4*(SF - 2*DE))) * (packet->coderate + 4)); /* Explicitely cast to double to keep precision of the division */ - - Tpayload = payloadSymbNb * Tsym; - - Tpacket = Tpreamble + Tpayload; - - return Tpacket; -} /* -------------------------------------------------------------------------- */ /* --- PUBLIC FUNCTIONS DEFINITION ----------------------------------------- */ @@ -218,7 +155,7 @@ enum jit_error_e jit_enqueue(struct jit_queue_s *queue, struct timeval *time, st case JIT_PKT_TYPE_DOWNLINK_CLASS_B: case JIT_PKT_TYPE_DOWNLINK_CLASS_C: packet_pre_delay = TX_START_DELAY + TX_JIT_DELAY; - packet_post_delay = time_on_air(packet, false) * 1000UL; /* in us */ + packet_post_delay = lgw_time_on_air(packet) * 1000UL; /* in us */ break; case JIT_PKT_TYPE_BEACON: /* As defined in LoRaWAN spec */ diff --git a/lora_pkt_fwd/src/lora_pkt_fwd.c b/lora_pkt_fwd/src/lora_pkt_fwd.c index 31a3743c..1c54d579 100644 --- a/lora_pkt_fwd/src/lora_pkt_fwd.c +++ b/lora_pkt_fwd/src/lora_pkt_fwd.c @@ -99,7 +99,7 @@ Maintainer: Michael Coracin #define MIN_LORA_PREAMB 6 /* minimum Lora preamble length for this application */ #define STD_LORA_PREAMB 8 #define MIN_FSK_PREAMB 3 /* minimum FSK preamble length for this application */ -#define STD_FSK_PREAMB 4 +#define STD_FSK_PREAMB 5 #define STATUS_SIZE 200 #define TX_BUFF_SIZE ((540 * NB_PKT_MAX) + 30 + STATUS_SIZE) @@ -225,7 +225,7 @@ static int parse_SX1301_configuration(const char * conf_file); static int parse_gateway_configuration(const char * conf_file); -static uint16_t crc_ccit(const uint8_t * data, unsigned size); +static uint16_t crc16(const uint8_t * data, unsigned size); static double difftimespec(struct timespec end, struct timespec beginning); @@ -256,7 +256,10 @@ static int parse_SX1301_configuration(const char * conf_file) { const char conf_obj_name[] = "SX1301_conf"; JSON_Value *root_val = NULL; JSON_Object *conf_obj = NULL; + JSON_Object *conf_lbt_obj = NULL; + JSON_Object *conf_lbtchan_obj = NULL; JSON_Value *val = NULL; + JSON_Array *conf_array = NULL; struct lgw_conf_board_s boardconf; struct lgw_conf_lbt_s lbtconf; struct lgw_conf_rxrf_s rfconf; @@ -298,77 +301,92 @@ static int parse_SX1301_configuration(const char * conf_file) { MSG("INFO: lorawan_public %d, clksrc %d\n", boardconf.lorawan_public, boardconf.clksrc); /* all parameters parsed, submitting configuration to the HAL */ if (lgw_board_setconf(boardconf) != LGW_HAL_SUCCESS) { - MSG("WARNING: Failed to configure board\n"); + MSG("ERROR: Failed to configure board\n"); + return -1; } - /* LBT struct*/ + /* set LBT configuration */ memset(&lbtconf, 0, sizeof lbtconf); /* initialize configuration structure */ - val = json_object_get_value(conf_obj, "lbt_cfg"); /* fetch value (if possible) */ - if (json_value_get_type(val) != JSONObject) { + conf_lbt_obj = json_object_get_object(conf_obj, "lbt_cfg"); /* fetch value (if possible) */ + if (conf_lbt_obj == NULL) { MSG("INFO: no configuration for LBT\n"); } else { - val = json_object_dotget_value(conf_obj, "lbt_cfg.enable"); /* fetch value (if possible) */ + val = json_object_get_value(conf_lbt_obj, "enable"); /* fetch value (if possible) */ if (json_value_get_type(val) == JSONBoolean) { lbtconf.enable = (bool)json_value_get_boolean(val); } else { MSG("WARNING: Data type for lbt_cfg.enable seems wrong, please check\n"); lbtconf.enable = false; } - val = json_object_dotget_value(conf_obj, "lbt_cfg.rssi_target"); /* fetch value (if possible) */ - if (json_value_get_type(val) == JSONNumber) { - lbtconf.rssi_target = (uint8_t)json_value_get_number(val); - } else { - MSG("WARNING: Data type for lbt_cfg.rssi_target seems wrong, please check\n"); - lbtconf.rssi_target = 0; - } - val = json_object_dotget_value(conf_obj, "lbt_cfg.nb_channel"); /* fetch value (if possible) */ - if (json_value_get_type(val) == JSONNumber) { - lbtconf.nb_channel = (uint8_t)json_value_get_number(val); - } else { - MSG("WARNING: Data type for lbt_cfg.nb_channel seems wrong, please check\n"); - lbtconf.nb_channel = 0; - } - val = json_object_dotget_value(conf_obj, "lbt_cfg.start_freq"); /* fetch value (if possible) */ - if (json_value_get_type(val) == JSONNumber) { - lbtconf.start_freq = (uint32_t)json_value_get_number(val); - } else { - MSG("WARNING: Data type for lbt_cfg.start_freq seems wrong, please check\n"); - lbtconf.start_freq = 0; - } - val = json_object_dotget_value(conf_obj, "lbt_cfg.scan_time_us"); /* fetch value (if possible) */ - if (json_value_get_type(val) == JSONNumber) { - lbtconf.scan_time_us = (uint32_t)json_value_get_number(val); - } else { - MSG("WARNING: Data type for lbt_cfg.scan_time_us seems wrong, please check\n"); - lbtconf.scan_time_us = 0; - } - val = json_object_dotget_value(conf_obj, "lbt_cfg.tx_delay_1ch_us"); /* fetch value (if possible) */ - if (json_value_get_type(val) == JSONNumber) { - lbtconf.tx_delay_1ch_us = (uint32_t)json_value_get_number(val); - } else { - MSG("WARNING: Data type for lbt_cfg.tx_delay_1ch_us seems wrong, please check\n"); - lbtconf.tx_delay_1ch_us = 0; - } - val = json_object_dotget_value(conf_obj, "lbt_cfg.tx_delay_2ch_us"); /* fetch value (if possible) */ - if (json_value_get_type(val) == JSONNumber) { - lbtconf.tx_delay_2ch_us = (uint32_t)json_value_get_number(val); + if (lbtconf.enable == true) { + val = json_object_get_value(conf_lbt_obj, "rssi_target"); /* fetch value (if possible) */ + if (json_value_get_type(val) == JSONNumber) { + lbtconf.rssi_target = (int8_t)json_value_get_number(val); + } else { + MSG("WARNING: Data type for lbt_cfg.rssi_target seems wrong, please check\n"); + lbtconf.rssi_target = 0; + } + val = json_object_get_value(conf_lbt_obj, "sx127x_rssi_offset"); /* fetch value (if possible) */ + if (json_value_get_type(val) == JSONNumber) { + lbtconf.rssi_offset = (int8_t)json_value_get_number(val); + } else { + MSG("WARNING: Data type for lbt_cfg.sx127x_rssi_offset seems wrong, please check\n"); + lbtconf.rssi_offset = 0; + } + /* set LBT channels configuration */ + conf_array = json_object_get_array(conf_lbt_obj, "chan_cfg"); + if (conf_array != NULL) { + lbtconf.nb_channel = json_array_get_count( conf_array ); + MSG("INFO: %u LBT channels configured\n", lbtconf.nb_channel); + } + for (i = 0; i < (int)lbtconf.nb_channel; i++) { + /* Sanity check */ + if (i >= LBT_CHANNEL_FREQ_NB) + { + MSG("ERROR: LBT channel %d not supported, skip it\n", i ); + break; + } + /* Get LBT channel configuration object from array */ + conf_lbtchan_obj = json_array_get_object(conf_array, i); + + /* Channel frequency */ + val = json_object_dotget_value(conf_lbtchan_obj, "freq_hz"); /* fetch value (if possible) */ + if (json_value_get_type(val) == JSONNumber) { + lbtconf.channels[i].freq_hz = (uint32_t)json_value_get_number(val); + } else { + MSG("WARNING: Data type for lbt_cfg.channels[%d].freq_hz seems wrong, please check\n", i); + lbtconf.channels[i].freq_hz = 0; + } + + /* Channel scan time */ + val = json_object_dotget_value(conf_lbtchan_obj, "scan_time_us"); /* fetch value (if possible) */ + if (json_value_get_type(val) == JSONNumber) { + lbtconf.channels[i].scan_time_us = (uint16_t)json_value_get_number(val); + } else { + MSG("WARNING: Data type for lbt_cfg.channels[%d].scan_time_us seems wrong, please check\n", i); + lbtconf.channels[i].scan_time_us = 0; + } + } + + /* all parameters parsed, submitting configuration to the HAL */ + if (lgw_lbt_setconf(lbtconf) != LGW_HAL_SUCCESS) { + MSG("ERROR: Failed to configure LBT\n"); + return -1; + } } else { - MSG("WARNING: Data type for lbt_cfg.tx_delay_2ch_us seems wrong, please check\n"); - lbtconf.tx_delay_2ch_us = 0; + MSG("INFO: LBT is disabled\n"); } } - /* all parameters parsed, submitting configuration to the HAL */ - if (lgw_lbt_setconf(lbtconf) != LGW_HAL_SUCCESS) { - MSG("WARNING: Failed to configure lbt\n"); - } /* set antenna gain configuration */ val = json_object_get_value(conf_obj, "antenna_gain"); /* fetch value (if possible) */ - if (json_value_get_type(val) == JSONNumber) { - antenna_gain = (int8_t)json_value_get_number(val); - } else { - MSG("WARNING: Data type for antenna_gain seems wrong, please check\n"); - antenna_gain = 0; + if (val != NULL) { + if (json_value_get_type(val) == JSONNumber) { + antenna_gain = (int8_t)json_value_get_number(val); + } else { + MSG("WARNING: Data type for antenna_gain seems wrong, please check\n"); + antenna_gain = 0; + } } MSG("INFO: antenna_gain %d dBi\n", antenna_gain); @@ -391,42 +409,47 @@ static int parse_SX1301_configuration(const char * conf_file) { MSG("WARNING: Data type for %s[%d] seems wrong, please check\n", param_name, i); txlut.lut[i].pa_gain = 0; } - snprintf(param_name, sizeof param_name, "tx_lut_%i.dac_gain", i); - val = json_object_dotget_value(conf_obj, param_name); - if (json_value_get_type(val) == JSONNumber) { - txlut.lut[i].dac_gain = (uint8_t)json_value_get_number(val); - } else { - txlut.lut[i].dac_gain = 3; /* This is the only dac_gain supported for now */ - } - snprintf(param_name, sizeof param_name, "tx_lut_%i.dig_gain", i); - val = json_object_dotget_value(conf_obj, param_name); - if (json_value_get_type(val) == JSONNumber) { - txlut.lut[i].dig_gain = (uint8_t)json_value_get_number(val); - } else { + snprintf(param_name, sizeof param_name, "tx_lut_%i.dac_gain", i); + val = json_object_dotget_value(conf_obj, param_name); + if (json_value_get_type(val) == JSONNumber) { + txlut.lut[i].dac_gain = (uint8_t)json_value_get_number(val); + } else { + txlut.lut[i].dac_gain = 3; /* This is the only dac_gain supported for now */ + } + snprintf(param_name, sizeof param_name, "tx_lut_%i.dig_gain", i); + val = json_object_dotget_value(conf_obj, param_name); + if (json_value_get_type(val) == JSONNumber) { + txlut.lut[i].dig_gain = (uint8_t)json_value_get_number(val); + } else { MSG("WARNING: Data type for %s[%d] seems wrong, please check\n", param_name, i); - txlut.lut[i].dig_gain = 0; - } - snprintf(param_name, sizeof param_name, "tx_lut_%i.mix_gain", i); - val = json_object_dotget_value(conf_obj, param_name); - if (json_value_get_type(val) == JSONNumber) { - txlut.lut[i].mix_gain = (uint8_t)json_value_get_number(val); - } else { + txlut.lut[i].dig_gain = 0; + } + snprintf(param_name, sizeof param_name, "tx_lut_%i.mix_gain", i); + val = json_object_dotget_value(conf_obj, param_name); + if (json_value_get_type(val) == JSONNumber) { + txlut.lut[i].mix_gain = (uint8_t)json_value_get_number(val); + } else { MSG("WARNING: Data type for %s[%d] seems wrong, please check\n", param_name, i); - txlut.lut[i].mix_gain = 0; - } - snprintf(param_name, sizeof param_name, "tx_lut_%i.rf_power", i); - val = json_object_dotget_value(conf_obj, param_name); - if (json_value_get_type(val) == JSONNumber) { - txlut.lut[i].rf_power = (int8_t)json_value_get_number(val); - } else { + txlut.lut[i].mix_gain = 0; + } + snprintf(param_name, sizeof param_name, "tx_lut_%i.rf_power", i); + val = json_object_dotget_value(conf_obj, param_name); + if (json_value_get_type(val) == JSONNumber) { + txlut.lut[i].rf_power = (int8_t)json_value_get_number(val); + } else { MSG("WARNING: Data type for %s[%d] seems wrong, please check\n", param_name, i); - txlut.lut[i].rf_power = 0; - } + txlut.lut[i].rf_power = 0; + } } /* all parameters parsed, submitting configuration to the HAL */ - MSG("INFO: Configuring TX LUT with %u indexes\n", txlut.size); + if (txlut.size > 0) { + MSG("INFO: Configuring TX LUT with %u indexes\n", txlut.size); if (lgw_txgain_setconf(&txlut) != LGW_HAL_SUCCESS) { - MSG("WARNING: Failed to configure concentrator TX Gain LUT\n"); + MSG("ERROR: Failed to configure concentrator TX Gain LUT\n"); + return -1; + } + } else { + MSG("WARNING: No TX gain LUT defined\n"); } /* set configuration for RF chains */ @@ -475,15 +498,19 @@ static int parse_SX1301_configuration(const char * conf_file) { if ((tx_freq_min[i] == 0) || (tx_freq_max[i] == 0)) { MSG("WARNING: no frequency range specified for TX rf chain %d\n", i); } + /* ... and the notch filter frequency to be set */ + snprintf(param_name, sizeof param_name, "radio_%i.tx_notch_freq", i); + rfconf.tx_notch_freq = (uint32_t)json_object_dotget_number(conf_obj, param_name); } } else { rfconf.tx_enable = false; } - MSG("INFO: radio %i enabled (type %s), center frequency %u, RSSI offset %f, tx enabled %d\n", i, str, rfconf.freq_hz, rfconf.rssi_offset, rfconf.tx_enable); + MSG("INFO: radio %i enabled (type %s), center frequency %u, RSSI offset %f, tx enabled %d, tx_notch_freq %u\n", i, str, rfconf.freq_hz, rfconf.rssi_offset, rfconf.tx_enable, rfconf.tx_notch_freq); } /* all parameters parsed, submitting configuration to the HAL */ if (lgw_rxrf_setconf(i, rfconf) != LGW_HAL_SUCCESS) { - MSG("WARNING: invalid configuration for radio %i\n", i); + MSG("ERROR: invalid configuration for radio %i\n", i); + return -1; } } @@ -516,7 +543,8 @@ static int parse_SX1301_configuration(const char * conf_file) { } /* all parameters parsed, submitting configuration to the HAL */ if (lgw_rxif_setconf(i, ifconf) != LGW_HAL_SUCCESS) { - MSG("WARNING: invalid configuration for Lora multi-SF channel %i\n", i); + MSG("ERROR: invalid configuration for Lora multi-SF channel %i\n", i); + return -1; } } @@ -557,7 +585,8 @@ static int parse_SX1301_configuration(const char * conf_file) { MSG("INFO: Lora std channel> radio %i, IF %i Hz, %u Hz bw, SF %u\n", ifconf.rf_chain, ifconf.freq_hz, bw, sf); } if (lgw_rxif_setconf(8, ifconf) != LGW_HAL_SUCCESS) { - MSG("WARNING: invalid configuration for Lora standard channel\n"); + MSG("ERROR: invalid configuration for Lora standard channel\n"); + return -1; } } @@ -599,10 +628,12 @@ static int parse_SX1301_configuration(const char * conf_file) { MSG("INFO: FSK channel> radio %i, IF %i Hz, %u Hz bw, %u bps datarate\n", ifconf.rf_chain, ifconf.freq_hz, bw, ifconf.datarate); } if (lgw_rxif_setconf(9, ifconf) != LGW_HAL_SUCCESS) { - MSG("WARNING: invalid configuration for FSK channel\n"); + MSG("ERROR: invalid configuration for FSK channel\n"); + return -1; } } json_value_free(root_val); + return 0; } @@ -756,9 +787,9 @@ static int parse_gateway_configuration(const char * conf_file) { return 0; } -static uint16_t crc_ccit(const uint8_t * data, unsigned size) { - const uint16_t crc_poly = 0x1021; /* CCITT */ - const uint16_t init_val = 0xFFFF; /* CCITT */ +static uint16_t crc16(const uint8_t * data, unsigned size) { + const uint16_t crc_poly = 0x1021; + const uint16_t init_val = 0x0000; uint16_t x = init_val; unsigned i, j; @@ -776,28 +807,6 @@ static uint16_t crc_ccit(const uint8_t * data, unsigned size) { return x; } -#if 0 -static uint8_t crc8_ccit(const uint8_t * data, unsigned size) { - const uint8_t crc_poly = 0x87; /* CCITT */ - const uint8_t init_val = 0xFF; /* CCITT */ - uint8_t x = init_val; - unsigned i, j; - - if (data == NULL) { - return 0; - } - - for (i=0; i> 16); /* CRC of the optional beacon fileds */ - field_crc2 = crc_ccit((beacon_pkt.payload + 8), 7); + field_crc2 = crc16((beacon_pkt.payload + 8), 7); beacon_pkt.payload[15] = 0xFF & field_crc2; beacon_pkt.payload[16] = 0xFF & (field_crc2 >> 8); @@ -1954,7 +1978,7 @@ void thread_down(void) { beacon_pkt.payload[5] = 0xFF & (next_beacon_gps_time.tv_sec >> 24); /* calculate CRC */ - field_crc1 = crc_ccit(beacon_pkt.payload, 6); /* CRC for the first 6 bytes */ + field_crc1 = crc16(beacon_pkt.payload, 6); /* CRC for the first 6 bytes */ beacon_pkt.payload[6] = 0xFF & field_crc1; beacon_pkt.payload[7] = 0xFF & (field_crc1 >> 8); @@ -1975,7 +1999,7 @@ void thread_down(void) { /* display beacon payload */ MSG("--- Beacon queued (count_us=%u) - payload: ---\n", beacon_pkt.count_us); - for (i=0; i<24; ++i) { + for (i=0; i #include #include +#include -#define ERROR 0 -#define SUCCESS 1 #define STARTING_CAPACITY 15 #define ARRAY_MAX_CAPACITY 122880 /* 15*(2^13) */ #define OBJECT_MAX_CAPACITY 960 /* 15*(2^6) */ #define MAX_NESTING 19 -#define sizeof_token(a) (sizeof(a) - 1) -#define skip_char(str) ((*str)++) -#define skip_whitespaces(str) while (isspace(**str)) { skip_char(str); } +#define DOUBLE_SERIALIZATION_FORMAT "%f" + +#define SIZEOF_TOKEN(a) (sizeof(a) - 1) +#define SKIP_CHAR(str) ((*str)++) +#define SKIP_WHITESPACES(str) while (isspace(**str)) { SKIP_CHAR(str); } #define MAX(a, b) ((a) > (b) ? (a) : (b)) -#define parson_malloc(a) malloc(a) -#define parson_free(a) free((void*)a) -#define parson_realloc(a, b) realloc(a, b) +#undef malloc +#undef free + +static JSON_Malloc_Function parson_malloc = malloc; +static JSON_Free_Function parson_free = free; + +#define IS_CONT(b) (((unsigned char)(b) & 0xC0) == 0x80) /* is utf-8 continuation byte */ /* Type definitions */ typedef union json_value_value { - const char *string; + char *string; double number; JSON_Object *object; JSON_Array *array; @@ -59,7 +67,7 @@ struct json_value_t { }; struct json_object_t { - const char **names; + char **names; JSON_Value **values; size_t count; size_t capacity; @@ -74,35 +82,35 @@ struct json_array_t { /* Various */ static char * read_file(const char *filename); static void remove_comments(char *string, const char *start_token, const char *end_token); -static int try_realloc(void **ptr, size_t new_size); static char * parson_strndup(const char *string, size_t n); -static int is_utf(const unsigned char *string); +static char * parson_strdup(const char *string); +static int is_utf16_hex(const unsigned char *string); +static int num_bytes_in_utf8_sequence(unsigned char c); +static int verify_utf8_sequence(const unsigned char *string, int *len); +static int is_valid_utf8(const char *string, size_t string_len); static int is_decimal(const char *string, size_t length); /* JSON Object */ static JSON_Object * json_object_init(void); -static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value); -static int json_object_resize(JSON_Object *object, size_t capacity); +static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value); +static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity); static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n); static void json_object_free(JSON_Object *object); /* JSON Array */ static JSON_Array * json_array_init(void); -static int json_array_add(JSON_Array *array, JSON_Value *value); -static int json_array_resize(JSON_Array *array, size_t capacity); +static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value); +static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity); static void json_array_free(JSON_Array *array); /* JSON Value */ -static JSON_Value * json_value_init_object(void); -static JSON_Value * json_value_init_array(void); -static JSON_Value * json_value_init_string(const char *string); -static JSON_Value * json_value_init_number(double number); -static JSON_Value * json_value_init_boolean(int boolean); -static JSON_Value * json_value_init_null(void); +static JSON_Value * json_value_init_string_no_copy(char *string); /* Parser */ static void skip_quotes(const char **string); -static const char * get_processed_string(const char **string); +static int parse_utf_16(const char **unprocessed, char **processed); +static char * process_string(const char *input, size_t len); +static char * get_quoted_string(const char **string); static JSON_Value * parse_object_value(const char **string, size_t nesting); static JSON_Value * parse_array_value(const char **string, size_t nesting); static JSON_Value * parse_string_value(const char **string); @@ -111,15 +119,13 @@ static JSON_Value * parse_number_value(const char **string); static JSON_Value * parse_null_value(const char **string); static JSON_Value * parse_value(const char **string, size_t nesting); -/* Various */ -static int try_realloc(void **ptr, size_t new_size) { - void *reallocated_ptr = parson_realloc(*ptr, new_size); - if (!reallocated_ptr) - return ERROR; - *ptr = reallocated_ptr; - return SUCCESS; -} +/* Serialization */ +static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf); +static int json_serialize_string(const char *string, char *buf); +static int append_indent(char *buf, int level); +static int append_string(char *buf, const char *string); +/* Various */ static char * parson_strndup(const char *string, size_t n) { char *output_string = (char*)parson_malloc(n + 1); if (!output_string) @@ -129,10 +135,83 @@ static char * parson_strndup(const char *string, size_t n) { return output_string; } -static int is_utf(const unsigned char *s) { +static char * parson_strdup(const char *string) { + return parson_strndup(string, strlen(string)); +} + +static int is_utf16_hex(const unsigned char *s) { return isxdigit(s[0]) && isxdigit(s[1]) && isxdigit(s[2]) && isxdigit(s[3]); } +static int num_bytes_in_utf8_sequence(unsigned char c) { + if (c == 0xC0 || c == 0xC1 || c > 0xF4 || IS_CONT(c)) { + return 0; + } else if ((c & 0x80) == 0) { /* 0xxxxxxx */ + return 1; + } else if ((c & 0xE0) == 0xC0) { /* 110xxxxx */ + return 2; + } else if ((c & 0xF0) == 0xE0) { /* 1110xxxx */ + return 3; + } else if ((c & 0xF8) == 0xF0) { /* 11110xxx */ + return 4; + } + return 0; /* won't happen */ +} + +static int verify_utf8_sequence(const unsigned char *string, int *len) { + unsigned int cp = 0; + *len = num_bytes_in_utf8_sequence(string[0]); + + if (*len == 1) { + cp = string[0]; + } else if (*len == 2 && IS_CONT(string[1])) { + cp = string[0] & 0x1F; + cp = (cp << 6) | (string[1] & 0x3F); + } else if (*len == 3 && IS_CONT(string[1]) && IS_CONT(string[2])) { + cp = ((unsigned char)string[0]) & 0xF; + cp = (cp << 6) | (string[1] & 0x3F); + cp = (cp << 6) | (string[2] & 0x3F); + } else if (*len == 4 && IS_CONT(string[1]) && IS_CONT(string[2]) && IS_CONT(string[3])) { + cp = string[0] & 0x7; + cp = (cp << 6) | (string[1] & 0x3F); + cp = (cp << 6) | (string[2] & 0x3F); + cp = (cp << 6) | (string[3] & 0x3F); + } else { + return 0; + } + + /* overlong encodings */ + if ((cp < 0x80 && *len > 1) || + (cp < 0x800 && *len > 2) || + (cp < 0x10000 && *len > 3)) { + return 0; + } + + /* invalid unicode */ + if (cp > 0x10FFFF) { + return 0; + } + + /* surrogate halves */ + if (cp >= 0xD800 && cp <= 0xDFFF) { + return 0; + } + + return 1; +} + +static int is_valid_utf8(const char *string, size_t string_len) { + int len = 0; + const char *string_end = string + string_len; + while (string < string_end) { + if (!verify_utf8_sequence((const unsigned char*)string, &len)) { + return 0; + } + string += len; + } + return 1; +} + static int is_decimal(const char *string, size_t length) { if (length > 1 && string[0] == '0' && string[1] != '.') return 0; @@ -147,11 +226,17 @@ static int is_decimal(const char *string, size_t length) { static char * read_file(const char * filename) { FILE *fp = fopen(filename, "r"); size_t file_size; + long pos; char *file_contents; if (!fp) return NULL; fseek(fp, 0L, SEEK_END); - file_size = ftell(fp); + pos = ftell(fp); + if (pos < 0) { + fclose(fp); + return NULL; + } + file_size = pos; rewind(fp); file_contents = (char*)parson_malloc(sizeof(char) * (file_size + 1)); if (!file_contents) { @@ -177,7 +262,7 @@ static void remove_comments(char *string, const char *start_token, const char *e size_t start_token_len = strlen(start_token); size_t end_token_len = strlen(end_token); if (start_token_len == 0 || end_token_len == 0) - return; + return; while ((current_char = *string) != '\0') { if (current_char == '\\' && !escaped) { escaped = 1; @@ -186,15 +271,15 @@ static void remove_comments(char *string, const char *start_token, const char *e } else if (current_char == '\"' && !escaped) { in_string = !in_string; } else if (!in_string && strncmp(string, start_token, start_token_len) == 0) { - for(i = 0; i < start_token_len; i++) + for(i = 0; i < start_token_len; i++) string[i] = ' '; - string = string + start_token_len; + string = string + start_token_len; ptr = strstr(string, end_token); if (!ptr) return; for (i = 0; i < (ptr - string) + end_token_len; i++) string[i] = ' '; - string = ptr + end_token_len - 1; + string = ptr + end_token_len - 1; } escaped = 0; string++; @@ -206,40 +291,66 @@ static JSON_Object * json_object_init(void) { JSON_Object *new_obj = (JSON_Object*)parson_malloc(sizeof(JSON_Object)); if (!new_obj) return NULL; - new_obj->names = (const char**)NULL; + new_obj->names = (char**)NULL; new_obj->values = (JSON_Value**)NULL; new_obj->capacity = 0; new_obj->count = 0; return new_obj; } -static int json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { - size_t index; +static JSON_Status json_object_add(JSON_Object *object, const char *name, JSON_Value *value) { + size_t index = 0; + if (object == NULL || name == NULL || value == NULL) { + return JSONFailure; + } if (object->count >= object->capacity) { size_t new_capacity = MAX(object->capacity * 2, STARTING_CAPACITY); if (new_capacity > OBJECT_MAX_CAPACITY) - return ERROR; - if (json_object_resize(object, new_capacity) == ERROR) - return ERROR; + return JSONFailure; + if (json_object_resize(object, new_capacity) == JSONFailure) + return JSONFailure; } if (json_object_get_value(object, name) != NULL) - return ERROR; + return JSONFailure; index = object->count; - object->names[index] = parson_strndup(name, strlen(name)); - if (!object->names[index]) - return ERROR; + object->names[index] = parson_strdup(name); + if (object->names[index] == NULL) + return JSONFailure; object->values[index] = value; object->count++; - return SUCCESS; + return JSONSuccess; } -static int json_object_resize(JSON_Object *object, size_t capacity) { - if (try_realloc((void**)&object->names, capacity * sizeof(char*)) == ERROR) - return ERROR; - if (try_realloc((void**)&object->values, capacity * sizeof(JSON_Value*)) == ERROR) - return ERROR; - object->capacity = capacity; - return SUCCESS; +static JSON_Status json_object_resize(JSON_Object *object, size_t new_capacity) { + char **temp_names = NULL; + JSON_Value **temp_values = NULL; + + if ((object->names == NULL && object->values != NULL) || + (object->names != NULL && object->values == NULL) || + new_capacity == 0) { + return JSONFailure; /* Shouldn't happen */ + } + + temp_names = (char**)parson_malloc(new_capacity * sizeof(char*)); + if (temp_names == NULL) + return JSONFailure; + + temp_values = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); + if (temp_values == NULL) { + parson_free(temp_names); + return JSONFailure; + } + + if (object->names != NULL && object->values != NULL && object->count > 0) { + memcpy(temp_names, object->names, object->count * sizeof(char*)); + memcpy(temp_values, object->values, object->count * sizeof(JSON_Value*)); + } + parson_free(object->names); + parson_free(object->values); + object->names = temp_names; + object->values = temp_values; + object->capacity = new_capacity; + return JSONSuccess; } static JSON_Value * json_object_nget_value(const JSON_Object *object, const char *name, size_t n) { @@ -275,24 +386,35 @@ static JSON_Array * json_array_init(void) { return new_array; } -static int json_array_add(JSON_Array *array, JSON_Value *value) { +static JSON_Status json_array_add(JSON_Array *array, JSON_Value *value) { if (array->count >= array->capacity) { size_t new_capacity = MAX(array->capacity * 2, STARTING_CAPACITY); if (new_capacity > ARRAY_MAX_CAPACITY) - return ERROR; - if (!json_array_resize(array, new_capacity)) - return ERROR; + return JSONFailure; + if (json_array_resize(array, new_capacity) == JSONFailure) + return JSONFailure; } array->items[array->count] = value; array->count++; - return SUCCESS; + return JSONSuccess; } -static int json_array_resize(JSON_Array *array, size_t capacity) { - if (try_realloc((void**)&array->items, capacity * sizeof(JSON_Value*)) == ERROR) - return ERROR; - array->capacity = capacity; - return SUCCESS; +static JSON_Status json_array_resize(JSON_Array *array, size_t new_capacity) { + JSON_Value **new_items = NULL; + if (new_capacity == 0) { + return JSONFailure; + } + new_items = (JSON_Value**)parson_malloc(new_capacity * sizeof(JSON_Value*)); + if (new_items == NULL) { + return JSONFailure; + } + if (array->items != NULL && array->count > 0) { + memcpy(new_items, array->items, array->count * sizeof(JSON_Value*)); + } + parson_free(array->items); + array->items = new_items; + array->capacity = new_capacity; + return JSONSuccess; } static void json_array_free(JSON_Array *array) { @@ -303,33 +425,7 @@ static void json_array_free(JSON_Array *array) { } /* JSON Value */ -static JSON_Value * json_value_init_object(void) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONObject; - new_value->value.object = json_object_init(); - if (!new_value->value.object) { - parson_free(new_value); - return NULL; - } - return new_value; -} - -static JSON_Value * json_value_init_array(void) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONArray; - new_value->value.array = json_array_init(); - if (!new_value->value.array) { - parson_free(new_value); - return NULL; - } - return new_value; -} - -static JSON_Value * json_value_init_string(const char *string) { +static JSON_Value * json_value_init_string_no_copy(char *string) { JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); if (!new_value) return NULL; @@ -338,116 +434,128 @@ static JSON_Value * json_value_init_string(const char *string) { return new_value; } -static JSON_Value * json_value_init_number(double number) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONNumber; - new_value->value.number = number; - return new_value; -} - -static JSON_Value * json_value_init_boolean(int boolean) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONBoolean; - new_value->value.boolean = boolean; - return new_value; -} - -static JSON_Value * json_value_init_null(void) { - JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); - if (!new_value) - return NULL; - new_value->type = JSONNull; - return new_value; -} - /* Parser */ static void skip_quotes(const char **string) { - skip_char(string); + SKIP_CHAR(string); while (**string != '\"') { if (**string == '\0') return; if (**string == '\\') { - skip_char(string); + SKIP_CHAR(string); if (**string == '\0') return; } - skip_char(string); + SKIP_CHAR(string); } - skip_char(string); + SKIP_CHAR(string); } -/* Returns contents of a string inside double quotes and parses escaped - characters inside. - Example: "\u006Corem ipsum" -> lorem ipsum */ -static const char * get_processed_string(const char **string) { - const char *string_start = *string; - char *output, *processed_ptr, *unprocessed_ptr, current_char; - unsigned int utf_val; - skip_quotes(string); - if (**string == '\0') - return NULL; - output = parson_strndup(string_start + 1, *string - string_start - 2); - if (!output) - return NULL; - processed_ptr = unprocessed_ptr = output; - while (*unprocessed_ptr) { - current_char = *unprocessed_ptr; - if (current_char == '\\') { - unprocessed_ptr++; - current_char = *unprocessed_ptr; - switch (current_char) { - case '\"': case '\\': case '/': break; - case 'b': current_char = '\b'; break; - case 'f': current_char = '\f'; break; - case 'n': current_char = '\n'; break; - case 'r': current_char = '\r'; break; - case 't': current_char = '\t'; break; +static int parse_utf_16(const char **unprocessed, char **processed) { + unsigned int cp, lead, trail; + char *processed_ptr = *processed; + const char *unprocessed_ptr = *unprocessed; + unprocessed_ptr++; /* skips u */ + if (!is_utf16_hex((const unsigned char*)unprocessed_ptr) || sscanf(unprocessed_ptr, "%4x", &cp) == EOF) + return JSONFailure; + if (cp < 0x80) { + *processed_ptr = cp; /* 0xxxxxxx */ + } else if (cp < 0x800) { + *processed_ptr++ = ((cp >> 6) & 0x1F) | 0xC0; /* 110xxxxx */ + *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */ + } else if (cp < 0xD800 || cp > 0xDFFF) { + *processed_ptr++ = ((cp >> 12) & 0x0F) | 0xE0; /* 1110xxxx */ + *processed_ptr++ = ((cp >> 6) & 0x3F) | 0x80; /* 10xxxxxx */ + *processed_ptr = ((cp ) & 0x3F) | 0x80; /* 10xxxxxx */ + } else if (cp >= 0xD800 && cp <= 0xDBFF) { /* lead surrogate (0xD800..0xDBFF) */ + lead = cp; + unprocessed_ptr += 4; /* should always be within the buffer, otherwise previous sscanf would fail */ + if (*unprocessed_ptr++ != '\\' || *unprocessed_ptr++ != 'u' || /* starts with \u? */ + !is_utf16_hex((const unsigned char*)unprocessed_ptr) || + sscanf(unprocessed_ptr, "%4x", &trail) == EOF || + trail < 0xDC00 || trail > 0xDFFF) { /* valid trail surrogate? (0xDC00..0xDFFF) */ + return JSONFailure; + } + cp = ((((lead-0xD800)&0x3FF)<<10)|((trail-0xDC00)&0x3FF))+0x010000; + *processed_ptr++ = (((cp >> 18) & 0x07) | 0xF0); /* 11110xxx */ + *processed_ptr++ = (((cp >> 12) & 0x3F) | 0x80); /* 10xxxxxx */ + *processed_ptr++ = (((cp >> 6) & 0x3F) | 0x80); /* 10xxxxxx */ + *processed_ptr = (((cp ) & 0x3F) | 0x80); /* 10xxxxxx */ + } else { /* trail surrogate before lead surrogate */ + return JSONFailure; + } + unprocessed_ptr += 3; + *processed = processed_ptr; + *unprocessed = unprocessed_ptr; + return JSONSuccess; +} + + +/* Copies and processes passed string up to supplied length. +Example: "\u006Corem ipsum" -> lorem ipsum */ +static char* process_string(const char *input, size_t len) { + const char *input_ptr = input; + size_t initial_size = (len + 1) * sizeof(char); + size_t final_size = 0; + char *output = (char*)parson_malloc(initial_size); + char *output_ptr = output; + char *resized_output = NULL; + while ((*input_ptr != '\0') && (size_t)(input_ptr - input) < len) { + if (*input_ptr == '\\') { + input_ptr++; + switch (*input_ptr) { + case '\"': *output_ptr = '\"'; break; + case '\\': *output_ptr = '\\'; break; + case '/': *output_ptr = '/'; break; + case 'b': *output_ptr = '\b'; break; + case 'f': *output_ptr = '\f'; break; + case 'n': *output_ptr = '\n'; break; + case 'r': *output_ptr = '\r'; break; + case 't': *output_ptr = '\t'; break; case 'u': - unprocessed_ptr++; - if (!is_utf((const unsigned char*)unprocessed_ptr) || - sscanf(unprocessed_ptr, "%4x", &utf_val) == EOF) { - parson_free(output); - return NULL; - } - if (utf_val < 0x80) { - current_char = utf_val; - } else if (utf_val < 0x800) { - *processed_ptr++ = (utf_val >> 6) | 0xC0; - current_char = ((utf_val | 0x80) & 0xBF); - } else { - *processed_ptr++ = (utf_val >> 12) | 0xE0; - *processed_ptr++ = (((utf_val >> 6) | 0x80) & 0xBF); - current_char = ((utf_val | 0x80) & 0xBF); - } - unprocessed_ptr += 3; + if (parse_utf_16(&input_ptr, &output_ptr) == JSONFailure) + goto error; break; default: - parson_free(output); - return NULL; - break; + goto error; } - } else if ((unsigned char)current_char < 0x20) { /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */ - parson_free(output); - return NULL; + } else if ((unsigned char)*input_ptr < 0x20) { + goto error; /* 0x00-0x19 are invalid characters for json string (http://www.ietf.org/rfc/rfc4627.txt) */ + } else { + *output_ptr = *input_ptr; } - *processed_ptr = current_char; - processed_ptr++; - unprocessed_ptr++; + output_ptr++; + input_ptr++; } - *processed_ptr = '\0'; - if (try_realloc((void**)&output, strlen(output) + 1) == ERROR) + *output_ptr = '\0'; + /* resize to new length */ + final_size = (size_t)(output_ptr-output) + 1; + resized_output = (char*)parson_malloc(final_size); + if (resized_output == NULL) + goto error; + memcpy(resized_output, output, final_size); + parson_free(output); + return resized_output; +error: + parson_free(output); + return NULL; +} + +/* Return processed contents of a string between quotes and + skips passed argument to a matching quote. */ +static char * get_quoted_string(const char **string) { + const char *string_start = *string; + size_t string_len = 0; + skip_quotes(string); + if (**string == '\0') return NULL; - return output; + string_len = *string - string_start - 2; /* length without quotes */ + return process_string(string_start + 1, string_len); } static JSON_Value * parse_value(const char **string, size_t nesting) { if (nesting > MAX_NESTING) return NULL; - skip_whitespaces(string); + SKIP_WHITESPACES(string); switch (**string) { case '{': return parse_object_value(string, nesting + 1); @@ -471,49 +579,49 @@ static JSON_Value * parse_value(const char **string, size_t nesting) { static JSON_Value * parse_object_value(const char **string, size_t nesting) { JSON_Value *output_value = json_value_init_object(), *new_value = NULL; JSON_Object *output_object = json_value_get_object(output_value); - const char *new_key = NULL; - if (!output_value) + char *new_key = NULL; + if (output_value == NULL) return NULL; - skip_char(string); - skip_whitespaces(string); + SKIP_CHAR(string); + SKIP_WHITESPACES(string); if (**string == '}') { /* empty object */ - skip_char(string); + SKIP_CHAR(string); return output_value; } while (**string != '\0') { - new_key = get_processed_string(string); - skip_whitespaces(string); - if (!new_key || **string != ':') { + new_key = get_quoted_string(string); + SKIP_WHITESPACES(string); + if (new_key == NULL || **string != ':') { json_value_free(output_value); return NULL; } - skip_char(string); + SKIP_CHAR(string); new_value = parse_value(string, nesting); - if (!new_value) { + if (new_value == NULL) { parson_free(new_key); json_value_free(output_value); return NULL; } - if(!json_object_add(output_object, new_key, new_value)) { + if(json_object_add(output_object, new_key, new_value) == JSONFailure) { parson_free(new_key); parson_free(new_value); json_value_free(output_value); return NULL; } parson_free(new_key); - skip_whitespaces(string); + SKIP_WHITESPACES(string); if (**string != ',') break; - skip_char(string); - skip_whitespaces(string); + SKIP_CHAR(string); + SKIP_WHITESPACES(string); } - skip_whitespaces(string); + SKIP_WHITESPACES(string); if (**string != '}' || /* Trim object after parsing is over */ - json_object_resize(output_object, json_object_get_count(output_object)) == ERROR) { - json_value_free(output_value); - return NULL; + json_object_resize(output_object, json_object_get_count(output_object)) == JSONFailure) { + json_value_free(output_value); + return NULL; } - skip_char(string); + SKIP_CHAR(string); return output_value; } @@ -522,10 +630,10 @@ static JSON_Value * parse_array_value(const char **string, size_t nesting) { JSON_Array *output_array = json_value_get_array(output_value); if (!output_value) return NULL; - skip_char(string); - skip_whitespaces(string); + SKIP_CHAR(string); + SKIP_WHITESPACES(string); if (**string == ']') { /* empty array */ - skip_char(string); + SKIP_CHAR(string); return output_value; } while (**string != '\0') { @@ -534,37 +642,43 @@ static JSON_Value * parse_array_value(const char **string, size_t nesting) { json_value_free(output_value); return NULL; } - if(json_array_add(output_array, new_array_value) == ERROR) { + if(json_array_add(output_array, new_array_value) == JSONFailure) { parson_free(new_array_value); json_value_free(output_value); return NULL; } - skip_whitespaces(string); + SKIP_WHITESPACES(string); if (**string != ',') break; - skip_char(string); - skip_whitespaces(string); + SKIP_CHAR(string); + SKIP_WHITESPACES(string); } - skip_whitespaces(string); + SKIP_WHITESPACES(string); if (**string != ']' || /* Trim array after parsing is over */ - json_array_resize(output_array, json_array_get_count(output_array)) == ERROR) { - json_value_free(output_value); - return NULL; + json_array_resize(output_array, json_array_get_count(output_array)) == JSONFailure) { + json_value_free(output_value); + return NULL; } - skip_char(string); + SKIP_CHAR(string); return output_value; } static JSON_Value * parse_string_value(const char **string) { - const char *new_string = get_processed_string(string); - if (!new_string) + JSON_Value *value = NULL; + char *new_string = get_quoted_string(string); + if (new_string == NULL) + return NULL; + value = json_value_init_string_no_copy(new_string); + if (value == NULL) { + parson_free(new_string); return NULL; - return json_value_init_string(new_string); + } + return value; } static JSON_Value * parse_boolean_value(const char **string) { - size_t true_token_size = sizeof_token("true"); - size_t false_token_size = sizeof_token("false"); + size_t true_token_size = SIZEOF_TOKEN("true"); + size_t false_token_size = SIZEOF_TOKEN("false"); if (strncmp("true", *string, true_token_size) == 0) { *string += true_token_size; return json_value_init_boolean(1); @@ -589,7 +703,7 @@ static JSON_Value * parse_number_value(const char **string) { } static JSON_Value * parse_null_value(const char **string) { - size_t token_size = sizeof_token("null"); + size_t token_size = SIZEOF_TOKEN("null"); if (strncmp("null", *string, token_size) == 0) { *string += token_size; return json_value_init_null(); @@ -597,11 +711,180 @@ static JSON_Value * parse_null_value(const char **string) { return NULL; } +/* Serialization */ +#define APPEND_STRING(str) do { written = append_string(buf, (str)); \ + if (written < 0) { return -1; } \ + if (buf != NULL) { buf += written; } \ + written_total += written; } while(0) + +#define APPEND_INDENT(level) do { written = append_indent(buf, (level)); \ + if (written < 0) { return -1; } \ + if (buf != NULL) { buf += written; } \ + written_total += written; } while(0) + +static int json_serialize_to_buffer_r(const JSON_Value *value, char *buf, int level, int is_pretty, char *num_buf) +{ + const char *key = NULL, *string = NULL; + JSON_Value *temp_value = NULL; + JSON_Array *array = NULL; + JSON_Object *object = NULL; + size_t i = 0, count = 0; + double num = 0.0; + int written = -1, written_total = 0; + + switch (json_value_get_type(value)) { + case JSONArray: + array = json_value_get_array(value); + count = json_array_get_count(array); + APPEND_STRING("["); + if (count > 0 && is_pretty) + APPEND_STRING("\n"); + for (i = 0; i < count; i++) { + if (is_pretty) + APPEND_INDENT(level+1); + temp_value = json_array_get_value(array, i); + written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); + if (written < 0) + return -1; + if (buf != NULL) + buf += written; + written_total += written; + if (i < (count - 1)) + APPEND_STRING(","); + if (is_pretty) + APPEND_STRING("\n"); + } + if (count > 0 && is_pretty) + APPEND_INDENT(level); + APPEND_STRING("]"); + return written_total; + case JSONObject: + object = json_value_get_object(value); + count = json_object_get_count(object); + APPEND_STRING("{"); + if (count > 0 && is_pretty) + APPEND_STRING("\n"); + for (i = 0; i < count; i++) { + key = json_object_get_name(object, i); + if (is_pretty) + APPEND_INDENT(level+1); + written = json_serialize_string(key, buf); + if (written < 0) + return -1; + if (buf != NULL) + buf += written; + written_total += written; + APPEND_STRING(":"); + if (is_pretty) + APPEND_STRING(" "); + temp_value = json_object_get_value(object, key); + written = json_serialize_to_buffer_r(temp_value, buf, level+1, is_pretty, num_buf); + if (written < 0) + return -1; + if (buf != NULL) + buf += written; + written_total += written; + if (i < (count - 1)) + APPEND_STRING(","); + if (is_pretty) + APPEND_STRING("\n"); + } + if (count > 0 && is_pretty) + APPEND_INDENT(level); + APPEND_STRING("}"); + return written_total; + case JSONString: + string = json_value_get_string(value); + written = json_serialize_string(string, buf); + if (written < 0) + return -1; + if (buf != NULL) + buf += written; + written_total += written; + return written_total; + case JSONBoolean: + if (json_value_get_boolean(value)) + APPEND_STRING("true"); + else + APPEND_STRING("false"); + return written_total; + case JSONNumber: + num = json_value_get_number(value); + if (buf != NULL) + num_buf = buf; + if (num == ((double)(int)num)) /* check if num is integer */ + written = sprintf(num_buf, "%d", (int)num); + else + written = sprintf(num_buf, DOUBLE_SERIALIZATION_FORMAT, num); + if (written < 0) + return -1; + if (buf != NULL) + buf += written; + written_total += written; + return written_total; + case JSONNull: + APPEND_STRING("null"); + return written_total; + case JSONError: + return -1; + default: + return -1; + } +} + +static int json_serialize_string(const char *string, char *buf) { + size_t i = 0, len = strlen(string); + char c = '\0'; + int written = -1, written_total = 0; + APPEND_STRING("\""); + for (i = 0; i < len; i++) { + c = string[i]; + switch (c) { + case '\"': APPEND_STRING("\\\""); break; + case '\\': APPEND_STRING("\\\\"); break; + case '/': APPEND_STRING("\\/"); break; /* to make json embeddable in xml\/html */ + case '\b': APPEND_STRING("\\b"); break; + case '\f': APPEND_STRING("\\f"); break; + case '\n': APPEND_STRING("\\n"); break; + case '\r': APPEND_STRING("\\r"); break; + case '\t': APPEND_STRING("\\t"); break; + default: + if (buf != NULL) { + buf[0] = c; + buf += 1; + } + written_total += 1; + break; + } + } + APPEND_STRING("\""); + return written_total; +} + +static int append_indent(char *buf, int level) { + int i; + int written = -1, written_total = 0; + for (i = 0; i < level; i++) { + APPEND_STRING(" "); + } + return written_total; +} + +static int append_string(char *buf, const char *string) { + if (buf == NULL) { + return (int)strlen(string); + } + return sprintf(buf, "%s", string); +} + +#undef APPEND_STRING +#undef APPEND_INDENT + /* Parser API */ JSON_Value * json_parse_file(const char *filename) { char *file_contents = read_file(filename); JSON_Value *output_value = NULL; - if (!file_contents) + if (file_contents == NULL) return NULL; output_value = json_parse_string(file_contents); parson_free(file_contents); @@ -611,7 +894,7 @@ JSON_Value * json_parse_file(const char *filename) { JSON_Value * json_parse_file_with_comments(const char *filename) { char *file_contents = read_file(filename); JSON_Value *output_value = NULL; - if (!file_contents) + if (file_contents == NULL) return NULL; output_value = json_parse_string_with_comments(file_contents); parson_free(file_contents); @@ -619,7 +902,10 @@ JSON_Value * json_parse_file_with_comments(const char *filename) { } JSON_Value * json_parse_string(const char *string) { - if (!string || (*string != '{' && *string != '[')) + if (string == NULL) + return NULL; + SKIP_WHITESPACES(&string); + if (*string != '{' && *string != '[') return NULL; return parse_value((const char**)&string, 0); } @@ -627,13 +913,13 @@ JSON_Value * json_parse_string(const char *string) { JSON_Value * json_parse_string_with_comments(const char *string) { JSON_Value *result = NULL; char *string_mutable_copy = NULL, *string_mutable_copy_ptr = NULL; - string_mutable_copy = parson_strndup(string, strlen(string)); - if (!string_mutable_copy) + string_mutable_copy = parson_strdup(string); + if (string_mutable_copy == NULL) return NULL; remove_comments(string_mutable_copy, "/*", "*/"); remove_comments(string_mutable_copy, "//", "\n"); string_mutable_copy_ptr = string_mutable_copy; - skip_whitespaces(&string_mutable_copy_ptr); + SKIP_WHITESPACES(&string_mutable_copy_ptr); if (*string_mutable_copy_ptr != '{' && *string_mutable_copy_ptr != '[') { parson_free(string_mutable_copy); return NULL; @@ -647,6 +933,8 @@ JSON_Value * json_parse_string_with_comments(const char *string) { /* JSON Object API */ JSON_Value * json_object_get_value(const JSON_Object *object, const char *name) { + if (object == NULL || name == NULL) + return NULL; return json_object_nget_value(object, name, strlen(name)); } @@ -780,3 +1068,698 @@ void json_value_free(JSON_Value *value) { } parson_free(value); } + +JSON_Value * json_value_init_object(void) { + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) + return NULL; + new_value->type = JSONObject; + new_value->value.object = json_object_init(); + if (!new_value->value.object) { + parson_free(new_value); + return NULL; + } + return new_value; +} + +JSON_Value * json_value_init_array(void) { + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) + return NULL; + new_value->type = JSONArray; + new_value->value.array = json_array_init(); + if (!new_value->value.array) { + parson_free(new_value); + return NULL; + } + return new_value; +} + +JSON_Value * json_value_init_string(const char *string) { + char *copy = NULL; + JSON_Value *value; + size_t string_len = 0; + if (string == NULL) + return NULL; + string_len = strlen(string); + if (!is_valid_utf8(string, string_len)) + return NULL; + copy = parson_strndup(string, string_len); + if (copy == NULL) + return NULL; + value = json_value_init_string_no_copy(copy); + if (value == NULL) + parson_free(copy); + return value; +} + +JSON_Value * json_value_init_number(double number) { + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) + return NULL; + new_value->type = JSONNumber; + new_value->value.number = number; + return new_value; +} + +JSON_Value * json_value_init_boolean(int boolean) { + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) + return NULL; + new_value->type = JSONBoolean; + new_value->value.boolean = boolean ? 1 : 0; + return new_value; +} + +JSON_Value * json_value_init_null(void) { + JSON_Value *new_value = (JSON_Value*)parson_malloc(sizeof(JSON_Value)); + if (!new_value) + return NULL; + new_value->type = JSONNull; + return new_value; +} + +JSON_Value * json_value_deep_copy(const JSON_Value *value) { + size_t i = 0; + JSON_Value *return_value = NULL, *temp_value_copy = NULL, *temp_value = NULL; + const char *temp_string = NULL, *temp_key = NULL; + char *temp_string_copy = NULL; + JSON_Array *temp_array = NULL, *temp_array_copy = NULL; + JSON_Object *temp_object = NULL, *temp_object_copy = NULL; + + switch (json_value_get_type(value)) { + case JSONArray: + temp_array = json_value_get_array(value); + return_value = json_value_init_array(); + if (return_value == NULL) + return NULL; + temp_array_copy = json_value_get_array(return_value); + for (i = 0; i < json_array_get_count(temp_array); i++) { + temp_value = json_array_get_value(temp_array, i); + temp_value_copy = json_value_deep_copy(temp_value); + if (temp_value_copy == NULL) { + json_value_free(return_value); + return NULL; + } + if (json_array_add(temp_array_copy, temp_value_copy) == JSONFailure) { + json_value_free(return_value); + json_value_free(temp_value_copy); + return NULL; + } + } + return return_value; + case JSONObject: + temp_object = json_value_get_object(value); + return_value = json_value_init_object(); + if (return_value == NULL) + return NULL; + temp_object_copy = json_value_get_object(return_value); + for (i = 0; i < json_object_get_count(temp_object); i++) { + temp_key = json_object_get_name(temp_object, i); + temp_value = json_object_get_value(temp_object, temp_key); + temp_value_copy = json_value_deep_copy(temp_value); + if (temp_value_copy == NULL) { + json_value_free(return_value); + return NULL; + } + if (json_object_add(temp_object_copy, temp_key, temp_value_copy) == JSONFailure) { + json_value_free(return_value); + json_value_free(temp_value_copy); + return NULL; + } + } + return return_value; + case JSONBoolean: + return json_value_init_boolean(json_value_get_boolean(value)); + case JSONNumber: + return json_value_init_number(json_value_get_number(value)); + case JSONString: + temp_string = json_value_get_string(value); + temp_string_copy = parson_strdup(temp_string); + if (temp_string_copy == NULL) + return NULL; + return_value = json_value_init_string_no_copy(temp_string_copy); + if (return_value == NULL) + parson_free(temp_string_copy); + return return_value; + case JSONNull: + return json_value_init_null(); + case JSONError: + return NULL; + default: + return NULL; + } +} + +size_t json_serialization_size(const JSON_Value *value) { + char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ + int res = json_serialize_to_buffer_r(value, NULL, 0, 0, num_buf); + return res < 0 ? 0 : (size_t)(res + 1); +} + +JSON_Status json_serialize_to_buffer(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { + int written = -1; + size_t needed_size_in_bytes = json_serialization_size(value); + if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) { + return JSONFailure; + } + written = json_serialize_to_buffer_r(value, buf, 0, 0, NULL); + if (written < 0) + return JSONFailure; + return JSONSuccess; +} + +JSON_Status json_serialize_to_file(const JSON_Value *value, const char *filename) { + JSON_Status return_code = JSONSuccess; + FILE *fp = NULL; + char *serialized_string = json_serialize_to_string(value); + if (serialized_string == NULL) { + return JSONFailure; + } + fp = fopen (filename, "w"); + if (fp != NULL) { + if (fputs (serialized_string, fp) == EOF) { + return_code = JSONFailure; + } + if (fclose (fp) == EOF) { + return_code = JSONFailure; + } + } + json_free_serialized_string(serialized_string); + return return_code; +} + +char * json_serialize_to_string(const JSON_Value *value) { + JSON_Status serialization_result = JSONFailure; + size_t buf_size_bytes = json_serialization_size(value); + char *buf = NULL; + if (buf_size_bytes == 0) { + return NULL; + } + buf = (char*)parson_malloc(buf_size_bytes); + if (buf == NULL) + return NULL; + serialization_result = json_serialize_to_buffer(value, buf, buf_size_bytes); + if (serialization_result == JSONFailure) { + json_free_serialized_string(buf); + return NULL; + } + return buf; +} + +size_t json_serialization_size_pretty(const JSON_Value *value) { + char num_buf[1100]; /* recursively allocating buffer on stack is a bad idea, so let's do it only once */ + int res = json_serialize_to_buffer_r(value, NULL, 0, 1, num_buf); + return res < 0 ? 0 : (size_t)(res + 1); +} + +JSON_Status json_serialize_to_buffer_pretty(const JSON_Value *value, char *buf, size_t buf_size_in_bytes) { + int written = -1; + size_t needed_size_in_bytes = json_serialization_size_pretty(value); + if (needed_size_in_bytes == 0 || buf_size_in_bytes < needed_size_in_bytes) + return JSONFailure; + written = json_serialize_to_buffer_r(value, buf, 0, 1, NULL); + if (written < 0) + return JSONFailure; + return JSONSuccess; +} + +JSON_Status json_serialize_to_file_pretty(const JSON_Value *value, const char *filename) { + JSON_Status return_code = JSONSuccess; + FILE *fp = NULL; + char *serialized_string = json_serialize_to_string_pretty(value); + if (serialized_string == NULL) { + return JSONFailure; + } + fp = fopen (filename, "w"); + if (fp != NULL) { + if (fputs (serialized_string, fp) == EOF) { + return_code = JSONFailure; + } + if (fclose (fp) == EOF) { + return_code = JSONFailure; + } + } + json_free_serialized_string(serialized_string); + return return_code; +} + +char * json_serialize_to_string_pretty(const JSON_Value *value) { + JSON_Status serialization_result = JSONFailure; + size_t buf_size_bytes = json_serialization_size_pretty(value); + char *buf = NULL; + if (buf_size_bytes == 0) { + return NULL; + } + buf = (char*)parson_malloc(buf_size_bytes); + if (buf == NULL) + return NULL; + serialization_result = json_serialize_to_buffer_pretty(value, buf, buf_size_bytes); + if (serialization_result == JSONFailure) { + json_free_serialized_string(buf); + return NULL; + } + return buf; +} + +void json_free_serialized_string(char *string) { + parson_free(string); +} + +JSON_Status json_array_remove(JSON_Array *array, size_t ix) { + JSON_Value *temp_value = NULL; + size_t last_element_ix = 0; + if (array == NULL || ix >= json_array_get_count(array)) { + return JSONFailure; + } + last_element_ix = json_array_get_count(array) - 1; + json_value_free(json_array_get_value(array, ix)); + if (ix != last_element_ix) { /* Replace value with one from the end of array */ + temp_value = json_array_get_value(array, last_element_ix); + if (temp_value == NULL) { + return JSONFailure; + } + array->items[ix] = temp_value; + } + array->count -= 1; + return JSONSuccess; +} + +JSON_Status json_array_replace_value(JSON_Array *array, size_t ix, JSON_Value *value) { + if (array == NULL || value == NULL || ix >= json_array_get_count(array)) { + return JSONFailure; + } + json_value_free(json_array_get_value(array, ix)); + array->items[ix] = value; + return JSONSuccess; +} + +JSON_Status json_array_replace_string(JSON_Array *array, size_t i, const char* string) { + JSON_Value *value = json_value_init_string(string); + if (value == NULL) + return JSONFailure; + if (json_array_replace_value(array, i, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_replace_number(JSON_Array *array, size_t i, double number) { + JSON_Value *value = json_value_init_number(number); + if (value == NULL) + return JSONFailure; + if (json_array_replace_value(array, i, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_replace_boolean(JSON_Array *array, size_t i, int boolean) { + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) + return JSONFailure; + if (json_array_replace_value(array, i, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_replace_null(JSON_Array *array, size_t i) { + JSON_Value *value = json_value_init_null(); + if (value == NULL) + return JSONFailure; + if (json_array_replace_value(array, i, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_clear(JSON_Array *array) { + size_t i = 0; + if (array == NULL) + return JSONFailure; + for (i = 0; i < json_array_get_count(array); i++) { + json_value_free(json_array_get_value(array, i)); + } + array->count = 0; + return JSONSuccess; +} + +JSON_Status json_array_append_value(JSON_Array *array, JSON_Value *value) { + if (array == NULL || value == NULL) + return JSONFailure; + return json_array_add(array, value); +} + +JSON_Status json_array_append_string(JSON_Array *array, const char *string) { + JSON_Value *value = json_value_init_string(string); + if (value == NULL) + return JSONFailure; + if (json_array_append_value(array, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_append_number(JSON_Array *array, double number) { + JSON_Value *value = json_value_init_number(number); + if (value == NULL) + return JSONFailure; + if (json_array_append_value(array, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_append_boolean(JSON_Array *array, int boolean) { + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) + return JSONFailure; + if (json_array_append_value(array, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_array_append_null(JSON_Array *array) { + JSON_Value *value = json_value_init_null(); + if (value == NULL) + return JSONFailure; + if (json_array_append_value(array, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_set_value(JSON_Object *object, const char *name, JSON_Value *value) { + size_t i = 0; + JSON_Value *old_value; + if (object == NULL || name == NULL || value == NULL) + return JSONFailure; + old_value = json_object_get_value(object, name); + if (old_value != NULL) { /* free and overwrite old value */ + json_value_free(old_value); + for (i = 0; i < json_object_get_count(object); i++) { + if (strcmp(object->names[i], name) == 0) { + object->values[i] = value; + return JSONSuccess; + } + } + } + /* add new key value pair */ + return json_object_add(object, name, value); +} + +JSON_Status json_object_set_string(JSON_Object *object, const char *name, const char *string) { + return json_object_set_value(object, name, json_value_init_string(string)); +} + +JSON_Status json_object_set_number(JSON_Object *object, const char *name, double number) { + return json_object_set_value(object, name, json_value_init_number(number)); +} + +JSON_Status json_object_set_boolean(JSON_Object *object, const char *name, int boolean) { + return json_object_set_value(object, name, json_value_init_boolean(boolean)); +} + +JSON_Status json_object_set_null(JSON_Object *object, const char *name) { + return json_object_set_value(object, name, json_value_init_null()); +} + +JSON_Status json_object_dotset_value(JSON_Object *object, const char *name, JSON_Value *value) { + const char *dot_pos = NULL; + char *current_name = NULL; + JSON_Object *temp_obj = NULL; + JSON_Value *new_value = NULL; + if (value == NULL || name == NULL || value == NULL) + return JSONFailure; + dot_pos = strchr(name, '.'); + if (dot_pos == NULL) { + return json_object_set_value(object, name, value); + } else { + current_name = parson_strndup(name, dot_pos - name); + temp_obj = json_object_get_object(object, current_name); + if (temp_obj == NULL) { + new_value = json_value_init_object(); + if (new_value == NULL) { + parson_free(current_name); + return JSONFailure; + } + if (json_object_add(object, current_name, new_value) == JSONFailure) { + json_value_free(new_value); + parson_free(current_name); + return JSONFailure; + } + temp_obj = json_object_get_object(object, current_name); + } + parson_free(current_name); + return json_object_dotset_value(temp_obj, dot_pos + 1, value); + } +} + +JSON_Status json_object_dotset_string(JSON_Object *object, const char *name, const char *string) { + JSON_Value *value = json_value_init_string(string); + if (value == NULL) + return JSONFailure; + if (json_object_dotset_value(object, name, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_number(JSON_Object *object, const char *name, double number) { + JSON_Value *value = json_value_init_number(number); + if (value == NULL) + return JSONFailure; + if (json_object_dotset_value(object, name, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_boolean(JSON_Object *object, const char *name, int boolean) { + JSON_Value *value = json_value_init_boolean(boolean); + if (value == NULL) + return JSONFailure; + if (json_object_dotset_value(object, name, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_dotset_null(JSON_Object *object, const char *name) { + JSON_Value *value = json_value_init_null(); + if (value == NULL) + return JSONFailure; + if (json_object_dotset_value(object, name, value) == JSONFailure) { + json_value_free(value); + return JSONFailure; + } + return JSONSuccess; +} + +JSON_Status json_object_remove(JSON_Object *object, const char *name) { + size_t i = 0, last_item_index = 0; + if (object == NULL || json_object_get_value(object, name) == NULL) + return JSONFailure; + last_item_index = json_object_get_count(object) - 1; + for (i = 0; i < json_object_get_count(object); i++) { + if (strcmp(object->names[i], name) == 0) { + parson_free(object->names[i]); + json_value_free(object->values[i]); + if (i != last_item_index) { /* Replace key value pair with one from the end */ + object->names[i] = object->names[last_item_index]; + object->values[i] = object->values[last_item_index]; + } + object->count -= 1; + return JSONSuccess; + } + } + return JSONFailure; /* No execution path should end here */ +} + +JSON_Status json_object_dotremove(JSON_Object *object, const char *name) { + const char *dot_pos = strchr(name, '.'); + char *current_name = NULL; + JSON_Object *temp_obj = NULL; + if (dot_pos == NULL) { + return json_object_remove(object, name); + } else { + current_name = parson_strndup(name, dot_pos - name); + temp_obj = json_object_get_object(object, current_name); + if (temp_obj == NULL) { + parson_free(current_name); + return JSONFailure; + } + parson_free(current_name); + return json_object_dotremove(temp_obj, dot_pos + 1); + } +} + +JSON_Status json_object_clear(JSON_Object *object) { + size_t i = 0; + if (object == NULL) { + return JSONFailure; + } + for (i = 0; i < json_object_get_count(object); i++) { + parson_free(object->names[i]); + json_value_free(object->values[i]); + } + object->count = 0; + return JSONSuccess; +} + +JSON_Status json_validate(const JSON_Value *schema, const JSON_Value *value) { + JSON_Value *temp_schema_value = NULL, *temp_value = NULL; + JSON_Array *schema_array = NULL, *value_array = NULL; + JSON_Object *schema_object = NULL, *value_object = NULL; + JSON_Value_Type schema_type = JSONError, value_type = JSONError; + const char *key = NULL; + size_t i = 0, count = 0; + if (schema == NULL || value == NULL) + return JSONFailure; + schema_type = json_value_get_type(schema); + value_type = json_value_get_type(value); + if (schema_type != value_type && schema_type != JSONNull) /* null represents all values */ + return JSONFailure; + switch (schema_type) { + case JSONArray: + schema_array = json_value_get_array(schema); + value_array = json_value_get_array(value); + count = json_array_get_count(schema_array); + if (count == 0) + return JSONSuccess; /* Empty array allows all types */ + /* Get first value from array, rest is ignored */ + temp_schema_value = json_array_get_value(schema_array, 0); + for (i = 0; i < json_array_get_count(value_array); i++) { + temp_value = json_array_get_value(value_array, i); + if (json_validate(temp_schema_value, temp_value) == 0) { + return JSONFailure; + } + } + return JSONSuccess; + case JSONObject: + schema_object = json_value_get_object(schema); + value_object = json_value_get_object(value); + count = json_object_get_count(schema_object); + if (count == 0) + return JSONSuccess; /* Empty object allows all objects */ + else if (json_object_get_count(value_object) < count) + return JSONFailure; /* Tested object mustn't have less name-value pairs than schema */ + for (i = 0; i < count; i++) { + key = json_object_get_name(schema_object, i); + temp_schema_value = json_object_get_value(schema_object, key); + temp_value = json_object_get_value(value_object, key); + if (temp_value == NULL) + return JSONFailure; + if (json_validate(temp_schema_value, temp_value) == JSONFailure) + return JSONFailure; + } + return JSONSuccess; + case JSONString: case JSONNumber: case JSONBoolean: case JSONNull: + return JSONSuccess; /* equality already tested before switch */ + case JSONError: default: + return JSONFailure; + } +} + +JSON_Status json_value_equals(const JSON_Value *a, const JSON_Value *b) { + JSON_Object *a_object = NULL, *b_object = NULL; + JSON_Array *a_array = NULL, *b_array = NULL; + const char *a_string = NULL, *b_string = NULL; + const char *key = NULL; + size_t a_count = 0, b_count = 0, i = 0; + JSON_Value_Type a_type, b_type; + a_type = json_value_get_type(a); + b_type = json_value_get_type(b); + if (a_type != b_type) { + return 0; + } + switch (a_type) { + case JSONArray: + a_array = json_value_get_array(a); + b_array = json_value_get_array(b); + a_count = json_array_get_count(a_array); + b_count = json_array_get_count(b_array); + if (a_count != b_count) { + return 0; + } + for (i = 0; i < a_count; i++) { + if (!json_value_equals(json_array_get_value(a_array, i), + json_array_get_value(b_array, i))) { + return 0; + } + } + return 1; + case JSONObject: + a_object = json_value_get_object(a); + b_object = json_value_get_object(b); + a_count = json_object_get_count(a_object); + b_count = json_object_get_count(b_object); + if (a_count != b_count) { + return 0; + } + for (i = 0; i < a_count; i++) { + key = json_object_get_name(a_object, i); + if (!json_value_equals(json_object_get_value(a_object, key), + json_object_get_value(b_object, key))) { + return 0; + } + } + return 1; + case JSONString: + a_string = json_value_get_string(a); + b_string = json_value_get_string(b); + return strcmp(a_string, b_string) == 0; + case JSONBoolean: + return json_value_get_boolean(a) == json_value_get_boolean(b); + case JSONNumber: + return fabs(json_value_get_number(a) - json_value_get_number(b)) < 0.000001; /* EPSILON */ + case JSONError: + return 1; + case JSONNull: + return 1; + default: + return 1; + } +} + +JSON_Value_Type json_type(const JSON_Value *value) { + return json_value_get_type(value); +} + +JSON_Object * json_object (const JSON_Value *value) { + return json_value_get_object(value); +} + +JSON_Array * json_array (const JSON_Value *value) { + return json_value_get_array(value); +} + +const char * json_string (const JSON_Value *value) { + return json_value_get_string(value); +} + +double json_number (const JSON_Value *value) { + return json_value_get_number(value); +} + +int json_boolean(const JSON_Value *value) { + return json_value_get_boolean(value); +} + +void json_set_allocation_functions(JSON_Malloc_Function malloc_fun, JSON_Free_Function free_fun) { + parson_malloc = malloc_fun; + parson_free = free_fun; +} diff --git a/readme.md b/readme.md index 439fa9e3..4d1d81e3 100644 --- a/readme.md +++ b/readme.md @@ -83,6 +83,14 @@ Please refer to the script header for more details. 5. Changelog ------------- +### v3.1.0 - 2016-09-07 ### + +* Updated "Listen-Before-Talk" JSON configuration to match with LBT rework. +* Added TX Notch Filter JSON configuration. +* Updated Parson library to latest version +* Fixed Class-B beacon CRC-16 calculation +* Removed JiT time_on_air local function, and use lgw_time_on_air() function + ### v3.0.0 - 2016-05-19 ### * Merged all different flavours of packet forwarder into one unique lora_pkt_fwd @@ -90,8 +98,8 @@ Please refer to the script header for more details. global_conf.json.XXX file provided in lora_pkt_fwd/cfg. * Added downlink "just-in-time" scheduling to optimize downlink capacity. * Updated Gateway <-> NetworkServer protocol to describe the new format of - "tx_ack" message. -* Added "Listen-Before-Talk" configuration. +"tx_ack" message. +* Added "Listen-Before-Talk" JSON configuration. * Splitted reset_pkt_fwd.sh script in 2 different scripts: - reset_lgw.sh, provided with the HAL (lora_gateway) - update_gwid.sh, provided with lora_pkt_fwd diff --git a/util_tx_test/src/util_tx_test.c b/util_tx_test/src/util_tx_test.c index aad76d83..00b0c03f 100644 --- a/util_tx_test/src/util_tx_test.c +++ b/util_tx_test/src/util_tx_test.c @@ -415,8 +415,10 @@ int main(int argc, char **argv) } /* Preamble size */ - memcpy((void *)(databuf + buff_index), (void *)",\"prea\":8", 9); - buff_index += 9; + if (strcmp(mod, "LORA") == 0) { + memcpy((void *)(databuf + buff_index), (void *)",\"prea\":8", 9); + buff_index += 9; + } /* payload size */ i = snprintf((char *)(databuf + buff_index), 12, ",\"size\":%i", payload_size);