23
23
#include <stdlib.h>
24
24
#include "string.h"
25
25
#include "esp_task_wdt.h"
26
+ #include "cJSON.h"
26
27
#include "xplr_lband.h"
27
28
#include "./../../../components/hpglib/src/common/xplr_common.h"
28
29
39
40
#define XPLRLBAND_CONSOLE (message , ...) do{} while(0)
40
41
#endif
41
42
43
+ #define XPLRLBAND_FREQ_REGION_EU "eu"
44
+ #define XPLRLBAND_FREQ_REGION_US "us"
45
+
42
46
/* ----------------------------------------------------------------
43
47
* STATIC TYPES
44
48
* -------------------------------------------------------------- */
@@ -83,6 +87,11 @@ typedef struct xplrLband_type {
83
87
* STATIC VARIABLES
84
88
* -------------------------------------------------------------- */
85
89
90
+ static const char * freqRegions [] = {
91
+ XPLRLBAND_FREQ_REGION_EU ,
92
+ XPLRLBAND_FREQ_REGION_US
93
+ };
94
+
86
95
static xplrLband_t lbandDvcs [XPLRLBAND_NUMOF_DEVICES ] = {NULL };
87
96
static int32_t ubxRet ;
88
97
static int32_t espRet ;
@@ -97,6 +106,10 @@ static esp_err_t xplrLbandPrivateConfigAllDefault(uint8_t dvcProfile, uint8_t i2
97
106
static esp_err_t xplrLbandPrivateSetDeviceConfig (uint8_t dvcProfile , uDeviceCfg_t * deviceSettings );
98
107
static esp_err_t xplrLbandPrivateSetNetworkConfig (uint8_t dvcProfile , uNetworkCfgGnss_t * deviceNetwork );
99
108
static int32_t xplrLbandPrivateAsyncStopper (uint8_t dvcProfile , int32_t handler );
109
+ static esp_err_t xplrLbandParseFrequencyFromMqtt (uint8_t dvcProfile ,
110
+ char * mqttPayload ,
111
+ xplrLbandRegion region ,
112
+ uint32_t * parsedFrequency );
100
113
101
114
/* ----------------------------------------------------------------
102
115
* STATIC CALLBACK FUNCTION PROTOTYPES
@@ -122,8 +135,7 @@ esp_err_t xplrLbandUbxlibDeinit(void)
122
135
}
123
136
124
137
esp_err_t xplrLbandStartDeviceDefaultSettings (uint8_t dvcProfile ,
125
- uint8_t i2cAddress ,
126
- uint32_t frequency ,
138
+ uint8_t i2cAddress ,
127
139
uDeviceHandle_t * destHandler )
128
140
{
129
141
if (!xplrHlprLocSrvcCheckDvcProfileValidity (dvcProfile , XPLRLBAND_NUMOF_DEVICES )) {
@@ -150,11 +162,6 @@ esp_err_t xplrLbandStartDeviceDefaultSettings(uint8_t dvcProfile,
150
162
return espRet ;
151
163
}
152
164
153
- espRet = xplrLbandSetFrequency (dvcProfile , frequency );
154
- if (espRet != ESP_OK ) {
155
- return espRet ;
156
- }
157
-
158
165
if (destHandler != NULL ) {
159
166
espRet = xplrLbandSendCorrectionDataAsyncStart (dvcProfile , destHandler );
160
167
if (espRet != ESP_OK ) {
@@ -166,8 +173,7 @@ esp_err_t xplrLbandStartDeviceDefaultSettings(uint8_t dvcProfile,
166
173
}
167
174
168
175
esp_err_t xplrLbandStartDevice (uint8_t dvcProfile ,
169
- xplrLbandDeviceCfg_t * dvcCfg ,
170
- uint32_t frequency ,
176
+ xplrLbandDeviceCfg_t * dvcCfg ,
171
177
uDeviceHandle_t * destHandler )
172
178
{
173
179
if (!xplrHlprLocSrvcCheckDvcProfileValidity (dvcProfile , XPLRLBAND_NUMOF_DEVICES )) {
@@ -199,11 +205,6 @@ esp_err_t xplrLbandStartDevice(uint8_t dvcProfile,
199
205
return espRet ;
200
206
}
201
207
202
- espRet = xplrLbandSetFrequency (dvcProfile , frequency );
203
- if (espRet != ESP_OK ) {
204
- return espRet ;
205
- }
206
-
207
208
if (destHandler != NULL ) {
208
209
espRet = xplrLbandSendCorrectionDataAsyncStart (dvcProfile , destHandler );
209
210
if (espRet != ESP_OK ) {
@@ -314,9 +315,9 @@ esp_err_t xplrLbandSetFrequency(uint8_t dvcProfile, uint32_t frequency)
314
315
return ESP_FAIL ;
315
316
}
316
317
317
- espRet = xplrLbandOptionSingleValSet (dvcProfile ,
318
+ espRet = xplrLbandOptionSingleValSet (dvcProfile ,
318
319
U_GNSS_CFG_VAL_KEY_ID_PMP_CENTER_FREQUENCY_U4 ,
319
- frequency ,
320
+ frequency ,
320
321
U_GNSS_CFG_VAL_LAYER_RAM );
321
322
if (espRet != ESP_OK ) {
322
323
return espRet ;
@@ -325,6 +326,50 @@ esp_err_t xplrLbandSetFrequency(uint8_t dvcProfile, uint32_t frequency)
325
326
return ESP_OK ;
326
327
}
327
328
329
+ esp_err_t xplrLbandSetFrequencyFromMqtt (uint8_t dvcProfile , char * mqttPayload , xplrLbandRegion region )
330
+ {
331
+ esp_err_t ret = ESP_OK ;
332
+ uint32_t frequency = 0 ;
333
+
334
+ ret = xplrLbandParseFrequencyFromMqtt (dvcProfile , mqttPayload , region , & frequency );
335
+
336
+ if (ret != ESP_OK || frequency == 0 ) {
337
+ XPLRLBAND_CONSOLE (E , "Could not parse frequency!" );
338
+ ret = ESP_FAIL ;
339
+ } else {
340
+ ret = xplrLbandSetFrequency (dvcProfile , frequency );
341
+ if (ret == ESP_OK ) {
342
+ XPLRLBAND_CONSOLE (D ,
343
+ "Set LBAND location: %s frequency: %d Hz successfully!" ,
344
+ freqRegions [region ],
345
+ frequency );
346
+ } else {
347
+ XPLRLBAND_CONSOLE (E , "Could not set LBAND location: %s frequency: %d Hz!" , frequency );
348
+ }
349
+ }
350
+
351
+ return ret ;
352
+ }
353
+
354
+ uint32_t xplrLbandGetFrequency (uint8_t dvcProfile )
355
+ {
356
+ uint32_t ret = 0 ;
357
+ esp_err_t espRet = ESP_OK ;
358
+
359
+ espRet = xplrLbandOptionSingleValGet (dvcProfile ,
360
+ U_GNSS_CFG_VAL_KEY_ID_PMP_CENTER_FREQUENCY_U4 ,
361
+ & ret ,
362
+ sizeof (ret ),
363
+ U_GNSS_CFG_VAL_LAYER_RAM );
364
+
365
+ if (espRet != ESP_OK ) {
366
+ XPLRLBAND_CONSOLE (E , "Could not read frequency from LBAND module!" );
367
+ ret = 0 ;
368
+ }
369
+
370
+ return ret ;
371
+ }
372
+
328
373
int32_t xplrLbandSendFormattedCommand (uint8_t dvcProfile , const char * pBuffer , size_t size )
329
374
{
330
375
if (!xplrHlprLocSrvcCheckDvcProfileValidity (dvcProfile , XPLRLBAND_NUMOF_DEVICES )) {
@@ -358,7 +403,7 @@ esp_err_t xplrLbandSendCorrectionDataAsyncStart(uint8_t dvcProfile, uDeviceHandl
358
403
return ESP_FAIL ;
359
404
}
360
405
361
- XPLRLBAND_CONSOLE (I , "Started LBAND Send Correction Data async." );
406
+ XPLRLBAND_CONSOLE (D , "Started LBAND Send Correction Data async." );
362
407
return ESP_OK ;
363
408
}
364
409
@@ -471,6 +516,59 @@ static int32_t xplrLbandPrivateAsyncStopper(uint8_t dvcProfile, int32_t handler)
471
516
return ubxRet ;
472
517
}
473
518
519
+ static esp_err_t xplrLbandParseFrequencyFromMqtt (uint8_t dvcProfile ,
520
+ char * mqttPayload ,
521
+ xplrLbandRegion region ,
522
+ uint32_t * parsedFrequency )
523
+ {
524
+ esp_err_t ret = ESP_OK ;
525
+ cJSON * json , * freqs , * jregion , * current , * frequency ;
526
+ double tmpfreq = 0 ;
527
+
528
+ if (!xplrHlprLocSrvcCheckDvcProfileValidity (dvcProfile , XPLRLBAND_NUMOF_DEVICES )) {
529
+ ret = ESP_FAIL ;
530
+ }
531
+
532
+ if (ret == ESP_OK ) {
533
+ json = cJSON_Parse (mqttPayload );
534
+
535
+ if (cJSON_HasObjectItem (json , "frequencies" )){
536
+ freqs = cJSON_GetObjectItem (json , "frequencies" );
537
+ if (cJSON_HasObjectItem (freqs , freqRegions [region ])) {
538
+ jregion = cJSON_GetObjectItem (freqs , freqRegions [region ]);
539
+ if (cJSON_HasObjectItem (jregion , "current" )) {
540
+ current = cJSON_GetObjectItem (jregion , "current" );
541
+ if (cJSON_HasObjectItem (current , "value" )) {
542
+ frequency = cJSON_GetObjectItem (current , "value" );
543
+ sscanf (cJSON_GetStringValue (frequency ), "%lf" , & tmpfreq );
544
+ * parsedFrequency = (uint32_t )((1e+6 ) * tmpfreq );
545
+ } else {
546
+ XPLRLBAND_CONSOLE (E , "Theres no frequency \"value\" object." );
547
+ * parsedFrequency = 0 ;
548
+ ret = ESP_FAIL ;
549
+ }
550
+ } else {
551
+ XPLRLBAND_CONSOLE (E , "Theres no \"current\" object." );
552
+ * parsedFrequency = 0 ;
553
+ ret = ESP_FAIL ;
554
+ }
555
+ } else {
556
+ XPLRLBAND_CONSOLE (E , "Theres no \"%s\" location object." , freqRegions [region ]);
557
+ * parsedFrequency = 0 ;
558
+ ret = ESP_FAIL ;
559
+ }
560
+ } else {
561
+ XPLRLBAND_CONSOLE (E , "Theres no \"frequencies\" object." );
562
+ * parsedFrequency = 0 ;
563
+ ret = ESP_FAIL ;
564
+ }
565
+
566
+ cJSON_Delete (json );
567
+ }
568
+
569
+ return ESP_OK ;
570
+ }
571
+
474
572
/* ----------------------------------------------------------------
475
573
* STATIC CALLBACK FUNCTION DEFINITIONS
476
574
* -------------------------------------------------------------- */
0 commit comments