-
Notifications
You must be signed in to change notification settings - Fork 1
/
appsrv.c
1343 lines (1164 loc) · 40.8 KB
/
appsrv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
@file appsrv.c
@brief TIMAC 2.0 API User Interface Collector API
Group: WCS LPC
$Target Device: DEVICES $
******************************************************************************
$License: BSD3 2016 $
******************************************************************************
$Release Name: PACKAGE NAME $
$Release Date: PACKAGE RELEASE DATE $
*****************************************************************************/
/******************************************************************************
Includes
*****************************************************************************/
#include "compiler.h"
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <stdint.h>
#include <inttypes.h>
#include "debug_helpers.h"
#include "collector.h"
#include "api_mac.h"
#include "api_mac_linux.h"
#include "llc.h"
#include "cllc.h"
#include "smsgs.h"
#include "log.h"
#include "fatal.h"
#include "appsrv.h"
#include "csf_linux.h"
#include "mutex.h"
#include "threads.h"
#include "timer.h"
#include "stream.h"
#include "stream_socket.h"
#include "stream_uart.h"
/******************************************************************************
Typedefs
*****************************************************************************/
struct appsrv_connection {
/*! Is this item busy (broadcasting) */
bool is_busy;
/*! If something has gone wrong this is set to true */
bool is_dead;
/*! Name for us in debug logs */
char *dbg_name;
/* What connection number is this? */
int connection_id;
/*! The socket interface to the gateway */
struct mt_msg_interface socket_interface;
/*! Thread id for the socket interface */
intptr_t thread_id_s2appsrv;
/*! Next connection in the list */
struct appsrv_connection *pNext;
};
/******************************************************************************
GLOBAL Variables
*****************************************************************************/
/*! The interface the API mac uses, points to either the socket or the uart */
struct mt_msg_interface *API_MAC_msg_interface;
/*! Generic template for all gateway connections */
struct socket_cfg appClient_socket_cfg;
/*! Configuration for apimac if using a socket (ie: npi server) */
struct socket_cfg npi_socket_cfg;
/*! UART configuration for apimac if talking to UART instead of npi */
struct uart_cfg uart_cfg;
/*! Generic template for all gateway interfaces
Note: These parameters can be modified via the ini file
*/
struct mt_msg_interface appClient_mt_interface_template = {
.dbg_name = "appClient",
.is_NPI = false,
.frame_sync = false,
.include_chksum = false,
.hndl = 0,
.s_cfg = NULL,
.u_cfg = NULL,
.rx_thread = 0,
.tx_frag_size = 0,
.retry_max = 0,
.frag_timeout_mSecs = 10000,
.intermsg_timeout_mSecs = 10000,
.intersymbol_timeout_mSecs = 100,
.srsp_timeout_mSecs = 300,
.stack_id = 0,
.len_2bytes = true,
.rx_handler_cookie = 0,
.is_dead = false,
.flush_timeout_mSecs = 100
};
/*! Generic template for uart interface
Note: These parameters can be modified via the ini file
*/
struct mt_msg_interface uart_mt_interface = {
.dbg_name = "uart",
.is_NPI = false,
.frame_sync = true,
.include_chksum = true,
.hndl = 0,
.s_cfg = NULL,
.u_cfg = &uart_cfg,
.rx_thread = 0,
.tx_frag_size = 0,
.retry_max = 0,
.frag_timeout_mSecs = 10000,
.intermsg_timeout_mSecs = 10000,
.intersymbol_timeout_mSecs = 100,
.srsp_timeout_mSecs = 300,
.stack_id = 0,
.len_2bytes = true,
.rx_handler_cookie = 0,
.is_dead = false,
.flush_timeout_mSecs = 100
};
/*! Template for apimac connection to npi server
Note: These parameters can be modified via the ini file
*/
struct mt_msg_interface npi_mt_interface = {
.dbg_name = "npi",
.is_NPI = false,
.frame_sync = true,
.include_chksum = true,
.hndl = 0,
.s_cfg = &npi_socket_cfg,
.u_cfg = NULL,
.rx_thread = 0,
.tx_frag_size = 0,
.retry_max = 0,
.frag_timeout_mSecs = 10000,
.intermsg_timeout_mSecs = 10000,
.intersymbol_timeout_mSecs = 100,
.srsp_timeout_mSecs = 300,
.stack_id = 0,
.len_2bytes = true,
.rx_handler_cookie = 0,
.is_dead = false,
.flush_timeout_mSecs = 100
};
/*******************************************************************
* LOCAL VARIABLES
********************************************************************/
static intptr_t all_connections_mutex;
static struct appsrv_connection *all_connections;
static intptr_t all_connections_mutex;
/*******************************************************************
* LOCAL FUNCTIONS
********************************************************************/
/*! Lock the list of gateway connections
Often used when modifying the list
*/
static void lock_connection_list(void)
{
MUTEX_lock(all_connections_mutex, -1);
}
/* See lock() above */
static void unlock_connection_list(void)
{
MUTEX_unLock(all_connections_mutex);
}
/*!
* @brief send a data confirm to the gateway
* @param status - the status value to send
*/
static void send_AppsrvTxDataCnf(int status)
{
int len = TX_DATA_CNF_LEN;
uint8_t *pBuff;
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_TX_DATA_CNF);
/* Create duplicate pointer to msg buffer for building */
pBuff = pMsg->iobuf + HEADER_LEN;
/* Put status in the msg buffer */
*pBuff++ = (uint8_t)(status & 0xFF);
*pBuff++ = (uint8_t)((status >> 8) & 0xFF);
*pBuff++ = (uint8_t)((status >> 16) & 0xFF);
*pBuff++ = (uint8_t)((status >> 24) & 0xFF);
/* Send msg */
MT_MSG_setDestIface(pMsg, &appClient_mt_interface_template);
MT_MSG_wrBuf(pMsg, NULL, len);
appsrv_broadcast(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
/*!
* @brief send a join confirm to the gateway
*/
static void send_AppSrvJoinPermitCnf(int status)
{
int len = JOIN_PERMIT_CNF_LEN;
uint8_t *pBuff;
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_SET_JOIN_PERMIT_CNF);
/* Create duplicate pointer to msg buffer for building */
pBuff = pMsg->iobuf + HEADER_LEN;
/* Put status in the msg buffer */
*pBuff++ = (uint8_t)(status & 0xFF);
*pBuff++ = (uint8_t)((status >> 8) & 0xFF);
*pBuff++ = (uint8_t)((status >> 16) & 0xFF);
*pBuff++ = (uint8_t)((status >> 24) & 0xFF);
/* Send msg */
MT_MSG_setDestIface(pMsg, &appClient_mt_interface_template);
MT_MSG_wrBuf(pMsg, NULL, len);
appsrv_broadcast(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
/*!
* @brief handle a data request from the gateway
* @param pCONN - where the request came from
* @param pIncomingMsg - the msg from the gateway
*/
static void appsrv_processTxDataReq(struct appsrv_connection *pCONN,
struct mt_msg *pIncomingMsg)
{
int status;
int ind = HEADER_LEN;
uint8_t msgId;
uint16_t shortAddr;
/* Parse msg */
msgId = (uint8_t)pIncomingMsg->iobuf[ind];
ind += 1;
shortAddr = (uint16_t)(pIncomingMsg->iobuf[ind]) |
(pIncomingMsg->iobuf[ind + 1] << 8);
ind += 2;
ApiMac_sAddr_t pDstAddr;
pDstAddr.addrMode = ApiMac_addrType_short;
pDstAddr.addr.shortAddr = shortAddr;
if (msgId == Smsgs_cmdIds_configReq)
{
uint16_t framecontrol;
uint32_t pollingInterval;
uint32_t reportingInterval;
uint8_t configStatus;
pollingInterval = (uint32_t)(pIncomingMsg->iobuf[ind]) |
(pIncomingMsg->iobuf[ind + 1] << 8);
ind += 2;
reportingInterval = (uint32_t)(pIncomingMsg->iobuf[ind]) |
(pIncomingMsg->iobuf[ind + 1] << 8);
ind += 2;
framecontrol = (uint16_t)(pIncomingMsg->iobuf[ind]) |
(pIncomingMsg->iobuf[ind + 1] << 8);
configStatus = Csf_sendConfigRequest(&pDstAddr, framecontrol, reportingInterval, pollingInterval);
LOG_printf(LOG_APPSRV_MSG_CONTENT, " Config-req sent\n");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "Config status: %x\n", configStatus);
}
else if (msgId == Smsgs_cmdIds_customCommand)
{
uint16_t length = ((pIncomingMsg->iobuf[ind]) |
(pIncomingMsg->iobuf[ind + 1] << 8)) + sizeof(uint16_t);
uint8_t *state = Csf_malloc(128); // fixme
int i = 0;
for(i=0; i < length; i++ ) {
*(state+i) = pIncomingMsg->iobuf[ind+i];
}
Csf_customCommand(&pDstAddr, state, length);
}
status = ApiMac_status_success;
send_AppsrvTxDataCnf(status);
}
static void appsrv_processRemoveDeviceReq(struct appsrv_connection *pCONN,
struct mt_msg *pIncomingMsg)
{
int ind = HEADER_LEN;
uint16_t shortAddr;
ApiMac_sAddr_t dstAddr;
Llc_deviceListItem_t devListItem;
ApiMac_status_t status;
shortAddr = (uint16_t)(pIncomingMsg->iobuf[ind]) |
(pIncomingMsg->iobuf[ind + 1] << 8);
dstAddr.addrMode = ApiMac_addrType_short;
dstAddr.addr.shortAddr = shortAddr;
if (Csf_getDevice(&dstAddr, &devListItem))
{
/* Send disassociation request to specified sensor */
ApiMac_mlmeDisassociateReq_t disassocReq;
memset(&disassocReq, 0, sizeof(ApiMac_mlmeDisassociateReq_t));
disassocReq.deviceAddress.addrMode = ApiMac_addrType_short;
disassocReq.deviceAddress.addr.shortAddr = devListItem.devInfo.shortAddress;
disassocReq.devicePanId = devListItem.devInfo.panID;
disassocReq.disassociateReason = ApiMac_disassocateReason_coord;
disassocReq.txIndirect = true;
status = ApiMac_mlmeDisassociateReq(&disassocReq);
LOG_printf(LOG_APPSRV_MSG_CONTENT, "Disassociate Sent, status: %x\n", status);
}
}
/*!
* @brief handle a join permit request from the gateway
* @param pCONN - where the request came from
* @param pIncomingMsg - the msg from the gateway
*/
static void appsrv_processSetJoinPermitReq(struct appsrv_connection *pCONN,
struct mt_msg *pIncomingMsg)
{
int status;
uint32_t duration;
duration = (uint32_t)(pIncomingMsg->iobuf[HEADER_LEN]) |
(pIncomingMsg->iobuf[HEADER_LEN + 1] << 8) |
(pIncomingMsg->iobuf[HEADER_LEN + 2] << 16) |
(pIncomingMsg->iobuf[HEADER_LEN + 3] << 24);
/* Set the join permit */
LOG_printf(LOG_APPSRV_MSG_CONTENT, "\nSending duration: 0x%x\n\n", duration);
status = Cllc_setJoinPermit(duration);
/* Send cnf msg */
LOG_printf(LOG_APPSRV_MSG_CONTENT, "\nSending permitCnf message\n\n");
send_AppSrvJoinPermitCnf(status);
}
/*!
* @brief handle a getnetwork info request from the gateway
* @param pCONN - where the request came from
*/
static void appsrv_processGetNwkInfoReq(struct appsrv_connection *pCONN)
{
Llc_netInfo_t networkInfo;
uint8_t *pBuff;
int len = NWK_INFO_REQ_LEN;
uint8_t status = (uint8_t)Csf_getNetworkInformation(&networkInfo);
uint8_t securityEnabled = CONFIG_SECURE;
uint8_t networkMode;
uint8_t state = Csf_getCllcState();
LOG_printf(LOG_APPSRV_MSG_CONTENT, "\nSending NwkCnf STATE = %x\n\n", state);
if (CONFIG_FH_ENABLE == true)
{
networkMode = FREQUENCY_HOPPING;
}
else
{
if ((CONFIG_MAC_SUPERFRAME_ORDER == 15) && (CONFIG_MAC_BEACON_ORDER == 15))
{
networkMode = NON_BEACON;
}
else
{
networkMode = BEACON_ENABLED;
}
}
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_GET_NWK_INFO_CNF);
/* Create duplicate pointer to msg buffer for building */
pBuff = pMsg->iobuf + HEADER_LEN;
/* Build msg */
*pBuff++ = status;
*pBuff++ = (uint8_t)(networkInfo.devInfo.panID & 0xFF);
*pBuff++ = (uint8_t)((networkInfo.devInfo.panID >> 8) & 0xFF);
*pBuff++ = (uint8_t)(networkInfo.devInfo.shortAddress & 0xFF);
*pBuff++ = (uint8_t)((networkInfo.devInfo.shortAddress >> 8) & 0xFF);
*pBuff++ = (uint8_t)(networkInfo.devInfo.extAddress[0]);
*pBuff++ = (uint8_t)(networkInfo.devInfo.extAddress[1]);
*pBuff++ = (uint8_t)(networkInfo.devInfo.extAddress[2]);
*pBuff++ = (uint8_t)(networkInfo.devInfo.extAddress[3]);
*pBuff++ = (uint8_t)(networkInfo.devInfo.extAddress[4]);
*pBuff++ = (uint8_t)(networkInfo.devInfo.extAddress[5]);
*pBuff++ = (uint8_t)(networkInfo.devInfo.extAddress[6]);
*pBuff++ = (uint8_t)(networkInfo.devInfo.extAddress[7]);
*pBuff++ = (uint8_t)(networkInfo.channel);
*pBuff++ = (uint8_t)(networkInfo.fh);
*pBuff++ = securityEnabled;
*pBuff++ = networkMode;
*pBuff++ = state;
/* Send msg */
MT_MSG_setDestIface(pMsg, &(pCONN->socket_interface));
MT_MSG_wrBuf(pMsg, NULL, len);
MT_MSG_txrx(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
/*!
* @brief Process incoming getDeviceArrayReq message
*
* @param pConn - the connection
*/
static void appsrv_processGetDeviceArrayReq(struct appsrv_connection *pCONN)
{
uint16_t n = 0;
uint8_t *pBuff;
Csf_deviceInformation_t *pDeviceInfo;
uint8_t status = ApiMac_status_success;
n = (uint16_t)Csf_getDeviceInformationList(&pDeviceInfo);
int len = DEV_ARRAY_HEAD_LEN + (DEV_ARRAY_INFO_LEN * n);
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_GET_DEVICE_ARRAY_CNF);
/* Create duplicate pointer to msg buffer for building */
pBuff = pMsg->iobuf + HEADER_LEN;
/* Build msg */
*pBuff++ = status;
*pBuff++ = (uint8_t)(n & 0xFF);
*pBuff++ = (uint8_t)((n >> 8) & 0xFF);
uint16_t x;
for (x = 0; x < n; x++)
{
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.panID & 0xFF);
*pBuff++ = (uint8_t)((pDeviceInfo[x].devInfo.panID >> 8) & 0xFF);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.shortAddress & 0xFF);
*pBuff++ = (uint8_t)((pDeviceInfo[x].devInfo.shortAddress >> 8) & 0xFF);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.extAddress[0]);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.extAddress[1]);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.extAddress[2]);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.extAddress[3]);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.extAddress[4]);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.extAddress[5]);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.extAddress[6]);
*pBuff++ = (uint8_t)(pDeviceInfo[x].devInfo.extAddress[7]);
*pBuff++ = (uint8_t)(pDeviceInfo[x].capInfo.panCoord);
*pBuff++ = (uint8_t)(pDeviceInfo[x].capInfo.ffd);
*pBuff++ = (uint8_t)(pDeviceInfo[x].capInfo.mainsPower);
*pBuff++ = (uint8_t)(pDeviceInfo[x].capInfo.rxOnWhenIdle);
*pBuff++ = (uint8_t)(pDeviceInfo[x].capInfo.security);
*pBuff++ = (uint8_t)(pDeviceInfo[x].capInfo.allocAddr);
}
/* Send msg */
MT_MSG_setDestIface(pMsg, &(pCONN->socket_interface));
MT_MSG_wrBuf(pMsg, NULL, len);
MT_MSG_txrx(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
if (pDeviceInfo)
{
Csf_freeDeviceInformationList(n, pDeviceInfo);
pDeviceInfo = NULL;
}
}
/******************************************************************************
Function Implementation
*****************************************************************************/
/*
Broadcast a message to all connections.
Public function in appsrv.h
*/
void appsrv_broadcast(struct mt_msg *pMsg)
{
struct appsrv_connection *pCONN;
struct mt_msg *pClone;
/* mark all connections as "ready to broadcast" */
lock_connection_list();
for(pCONN = all_connections ; pCONN ; pCONN = pCONN->pNext)
{
pCONN->is_busy = false;
}
unlock_connection_list();
next_connection:
/* find first connection in "ready-state"
* NOTE: this loop is funny we goto the top
* and we restart scanning from the head
* because ... while broadcasting a new
* connection may appear, or one may go away
*/
lock_connection_list();
for(pCONN = all_connections ; pCONN ; pCONN = pCONN->pNext)
{
/* this one is dead */
if(pCONN->is_dead)
{
continue;
}
/* is this one ready? */
if(pCONN->is_busy == false)
{
/* Yes we have found one */
pCONN->is_busy = true;
break;
}
}
unlock_connection_list();
/* Did we find a connection? */
if(pCONN)
{
/* we have a connection we can send */
pClone = MT_MSG_clone(pMsg);
if(pClone)
{
MT_MSG_setDestIface(pClone, &(pCONN->socket_interface));
MT_MSG_txrx(pClone);
MT_MSG_free(pClone);
}
/* leave this connection as 'busy'
* busy really means: "done"
* so that we do not repeat this connection
* we clean up the list later
*---
* Go back to the *FIRST* connection
* And search again.... from the top..
* Why? Because connections may have died...
*/
goto next_connection;
}
/* if we get here - we have no more connections to broadcast to */
/* mark all items as idle */
lock_connection_list();
for(pCONN = all_connections ; pCONN ; pCONN = pCONN->pNext)
{
pCONN->is_busy = false;
}
unlock_connection_list();
}
/*!
Csf module calls this function to inform the user/appClient
that the application has either started/restored the network
Public function defined in appsrv_Collector.h
*/
void appsrv_networkUpdate(bool restored, Llc_netInfo_t *networkInfo)
{
int len = NWK_INFO_IND_LEN;
uint8_t *pBuff;
uint8_t securityEnabled = CONFIG_SECURE;
uint8_t networkMode;
uint8_t state = Csf_getCllcState();
if (CONFIG_FH_ENABLE == true)
{
networkMode = FREQUENCY_HOPPING;
}
else
{
if ((CONFIG_MAC_SUPERFRAME_ORDER == 15) && (CONFIG_MAC_BEACON_ORDER == 15))
{
networkMode = NON_BEACON;
}
else
{
networkMode = BEACON_ENABLED;
}
}
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_NWK_INFO_IND);
/* Create duplicate pointer to msg buffer for building purposes */
pBuff = pMsg->iobuf + HEADER_LEN;
/* Build msg */
*pBuff++ = (uint8_t)(networkInfo->devInfo.panID & 0xFF);
*pBuff++ = (uint8_t)((networkInfo->devInfo.panID >> 8) & 0xFF);
*pBuff++ = (uint8_t)(networkInfo->devInfo.shortAddress & 0xFF);
*pBuff++ = (uint8_t)((networkInfo->devInfo.shortAddress >> 8) & 0xFF);
*pBuff++ = (uint8_t)(networkInfo->devInfo.extAddress[0]);
*pBuff++ = (uint8_t)(networkInfo->devInfo.extAddress[1]);
*pBuff++ = (uint8_t)(networkInfo->devInfo.extAddress[2]);
*pBuff++ = (uint8_t)(networkInfo->devInfo.extAddress[3]);
*pBuff++ = (uint8_t)(networkInfo->devInfo.extAddress[4]);
*pBuff++ = (uint8_t)(networkInfo->devInfo.extAddress[5]);
*pBuff++ = (uint8_t)(networkInfo->devInfo.extAddress[6]);
*pBuff++ = (uint8_t)(networkInfo->devInfo.extAddress[7]);
*pBuff++ = (uint8_t)(networkInfo->channel);
*pBuff++ = (uint8_t)(networkInfo->fh);
*pBuff++ = securityEnabled;
*pBuff++ = networkMode;
*pBuff++ = state;
/* Send msg */
MT_MSG_setDestIface(pMsg, &appClient_mt_interface_template);
MT_MSG_wrBuf(pMsg, NULL, len);
appsrv_broadcast(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
/*!
Csf module calls this function to inform the user/appClient
that a device has joined the network
Public function defined in appsrv_Collector.h
*/
void appsrv_deviceUpdate(Llc_deviceListItem_t *pDevListItem)
{
int len = DEVICE_JOINED_IND_LEN;
uint8_t *pBuff;
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_DEVICE_JOINED_IND);
/* Create duplicate pointer to msg buffer for building purposes */
pBuff = pMsg->iobuf + HEADER_LEN;
/* Build msg */
*pBuff++ = (uint8_t)(pDevListItem->devInfo.panID & 0xFF);
*pBuff++ = (uint8_t)((pDevListItem->devInfo.panID >> 8) & 0xFF);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.shortAddress & 0xFF);
*pBuff++ = (uint8_t)((pDevListItem->devInfo.shortAddress >> 8) & 0xFF);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.extAddress[0]);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.extAddress[1]);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.extAddress[2]);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.extAddress[3]);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.extAddress[4]);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.extAddress[5]);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.extAddress[6]);
*pBuff++ = (uint8_t)(pDevListItem->devInfo.extAddress[7]);
*pBuff++ = (uint8_t)(pDevListItem->capInfo.panCoord);
*pBuff++ = (uint8_t)(pDevListItem->capInfo.ffd);
*pBuff++ = (uint8_t)(pDevListItem->capInfo.mainsPower);
*pBuff++ = (uint8_t)(pDevListItem->capInfo.rxOnWhenIdle);
*pBuff++ = (uint8_t)(pDevListItem->capInfo.security);
*pBuff++ = (uint8_t)(pDevListItem->capInfo.allocAddr);
/* Send msg */
MT_MSG_setDestIface(pMsg, &appClient_mt_interface_template);
MT_MSG_wrBuf(pMsg, NULL, len);
appsrv_broadcast(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
/*
* @brief common code to handle sensor data messages
* @param pSrcAddr - address related to this message
*
* In the end, the message is sent to the gateway
*/
void appsrv_deviceRawDataUpdate(ApiMac_mcpsDataInd_t *pDataInd)
{
// Get the length (srcAddr + rssi + msdu.len)
uint16_t bufferLength = pDataInd->msdu.len + sizeof(pDataInd->rssi) + sizeof(ApiMac_sAddr_t);
uint8_t buffer[bufferLength];
memset(buffer, 0, bufferLength);
uint16_t idx = 0;
buffer[idx++] = pDataInd->srcAddr.addrMode;
if (pDataInd->srcAddr.addrMode == ApiMac_addrType_short)
{
buffer[idx++] = (uint8_t)(pDataInd->srcAddr.addr.shortAddr & 0xFF);
buffer[idx++] = (uint8_t)((pDataInd->srcAddr.addr.shortAddr >> 8) & 0xFF);
}
else if (pDataInd->srcAddr.addrMode == ApiMac_addrType_extended)
{
memcpy((buffer + idx), pDataInd->srcAddr.addr.extAddr, APIMAC_SADDR_EXT_LEN);
idx += APIMAC_SADDR_EXT_LEN;
}
buffer[idx++] = pDataInd->rssi;
memcpy((buffer + idx), pDataInd->msdu.p, pDataInd->msdu.len);
// initalize the message struct
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
bufferLength,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_DEVICE_DATA_RX_IND);
//add the ApiMac data to the message iobuf
uint16_t i;
for (i = 0; i < bufferLength; i++)
{
pMsg->iobuf[i + HEADER_LEN] = buffer[i];
}
// send the message buffer over a socket to the appclient
MT_MSG_setDestIface(pMsg, &appClient_mt_interface_template);
MT_MSG_wrBuf(pMsg, NULL, bufferLength);
appsrv_broadcast(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
void appsrv_send_removeDeviceRsp(void)
{
int len = REMOVE_DEVICE_RSP_LEN;
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_RMV_DEVICE_RSP);
/* Send msg */
MT_MSG_setDestIface(pMsg, &appClient_mt_interface_template);
MT_MSG_wrBuf(pMsg, NULL, len);
appsrv_broadcast(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
/*!
Csf module calls this function to inform the user/appClient
that a device is no longer active in the network
Public function defined in appsrv_Collector.h
*/
void appsrv_deviceNotActiveUpdate(ApiMac_deviceDescriptor_t *pDevInfo,
bool timeout)
{
int len = DEVICE_NOT_ACTIVE_LEN;
uint8_t *pBuff;
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_DEVICE_NOTACTIVE_UPDATE_IND);
/* Create duplicate pointer to msg buffer for building purposes */
pBuff = pMsg->iobuf + HEADER_LEN;
/* Build msg */
*pBuff++ = (uint8_t)(pDevInfo->panID & 0xFF);
*pBuff++ = (uint8_t)((pDevInfo->panID >> 8) & 0xFF);
*pBuff++ = (uint8_t)(pDevInfo->shortAddress & 0xFF);
*pBuff++ = (uint8_t)((pDevInfo->shortAddress >> 8) & 0xFF);
*pBuff++ = (uint8_t)(pDevInfo->extAddress[0]);
*pBuff++ = (uint8_t)(pDevInfo->extAddress[1]);
*pBuff++ = (uint8_t)(pDevInfo->extAddress[2]);
*pBuff++ = (uint8_t)(pDevInfo->extAddress[3]);
*pBuff++ = (uint8_t)(pDevInfo->extAddress[4]);
*pBuff++ = (uint8_t)(pDevInfo->extAddress[5]);
*pBuff++ = (uint8_t)(pDevInfo->extAddress[6]);
*pBuff++ = (uint8_t)(pDevInfo->extAddress[7]);
*pBuff++ = (uint8_t)timeout;
/* Send msg */
MT_MSG_setDestIface(pMsg, &appClient_mt_interface_template);
MT_MSG_wrBuf(pMsg, NULL, len);
appsrv_broadcast(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
/*!
TBD
Public function defined in appsrv_Collector.h
*/
void appsrv_stateChangeUpdate(Cllc_states_t state)
{
int len = STATE_CHG_IND_LEN;
struct mt_msg *pMsg;
pMsg = MT_MSG_alloc(
len,
MT_MSG_cmd0_areq(APPSRV_SYS_ID_RPC),
APPSRV_COLLECTOR_STATE_CNG_IND);
/* Build msg, no need for duplicate pointer*/
pMsg->iobuf[HEADER_LEN] = (uint8_t)(state & 0xFF);
MT_MSG_setDestIface(pMsg, &appClient_mt_interface_template);
MT_MSG_wrBuf(pMsg, NULL, len);
appsrv_broadcast(pMsg);
MT_MSG_free(pMsg);
pMsg = NULL;
}
/*********************************************************************
* Local Functions
*********************************************************************/
/*!
* @brief handle a request from a client.
* @param pCONN - the client connection details
* @param pMsg - the message we received.
*/
static void appsrv_handle_appClient_request( struct appsrv_connection *pCONN,
struct mt_msg *pMsg )
{
int subsys = _bitsXYof(pMsg->cmd0 , 4, 0);
int handled = true;
if (subsys != APPSRV_SYS_ID_RPC)
{
handled = false;
}
else
{
switch(pMsg->cmd1)
{
default:
handled = false;
break;
/*
* NOTE: ADD MORE ITEMS HERE TO EXTEND THE EXAMPLE
*/
case APPSRV_GET_DEVICE_ARRAY_REQ:
/* Rcvd data from Client */
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "rcvd get device array msg\n");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
appsrv_processGetDeviceArrayReq(pCONN);
break;
case APPSRV_GET_NWK_INFO_REQ:
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "getnwkinfo req message\n");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
appsrv_processGetNwkInfoReq(pCONN);
break;
case APPSRV_SET_JOIN_PERMIT_REQ:
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "rcvd join premit message\n ");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
appsrv_processSetJoinPermitReq(pCONN, pMsg);
break;
case APPSRV_TX_DATA_REQ:
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "rcvd req to send message to a device\n ");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
appsrv_processTxDataReq(pCONN, pMsg);
break;
case APPSRV_RMV_DEVICE_REQ:
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "rcvd req to remove a device\n ");
LOG_printf(LOG_APPSRV_MSG_CONTENT, "______________________________\n");
appsrv_processRemoveDeviceReq(pCONN, pMsg);
break;
}
}
if(!handled)
{
MT_MSG_log(LOG_ERROR, pMsg, "unknown msg\n");
}
}
/*
* @brief specific connection thread
* @param cookie - opaque parameter that is the connection details.
*
* The server thread creates these as needed
*
* This thread lives until the connection dies.
*/
static intptr_t s2appsrv_thread(intptr_t cookie)
{
struct appsrv_connection *pCONN;
struct mt_msg *pMsg;
int r;
char iface_name[30];
char star_line[30];
int star_line_char;
pCONN = (struct appsrv_connection *)(cookie);
if( pCONN == NULL )
{
BUG_HERE("pCONN is null?\n");
}
/* create our upstream interface */
(void)snprintf(iface_name,
sizeof(iface_name),
"s2u-%d-iface",
pCONN->connection_id);
pCONN->socket_interface.dbg_name = iface_name;
/* Create our interface */
r = MT_MSG_interfaceCreate(&(pCONN->socket_interface));
if(r != 0)
{
BUG_HERE("Cannot create socket interface?\n");
}
/* Add this connection to the list. */
lock_connection_list();
pCONN->pNext = all_connections;
pCONN->is_busy = false;
all_connections = pCONN;
unlock_connection_list();
star_line_char = 0;
/* Wait for messages to come in from the socket.. */
for(;;)
{
/* Did the other guy die? */
if(pCONN->is_dead)
{
break;
}
/* did the socket die? */
if(pCONN->socket_interface.is_dead)
{
pCONN->is_dead = true;
continue;
}
/* get our message */
pMsg = MT_MSG_LIST_remove(&(pCONN->socket_interface),
&(pCONN->socket_interface.rx_list), 1000);
if(pMsg == NULL)
{
/* must have timed out. */
continue;
}
pMsg->pLogPrefix = "web-request";
/* Print a *MARKER* line in the log to help trace this message */
star_line_char++;
/* Cycle through the letters AAAA, BBBB, CCCC .... */
star_line_char = star_line_char % 26;
memset((void *)(star_line),
star_line_char + 'A',
sizeof(star_line) - 1);
star_line[sizeof(star_line) - 1] = 0;
LOG_printf(LOG_DBG_MT_MSG_traffic, "START MSG: %s\n", star_line);
/* Actually process the request */
appsrv_handle_appClient_request(pCONN, pMsg);