3636 * 2019-05-14 17:21 zhangqianfu The first version
3737 *
3838 */
39+
3940#include <stdint.h>
4041#include <stddef.h>
4142#include <string.h>
4243
4344#include <osal.h>
4445#include <oc_lwm2m_al.h>
4546
46-
47- //#define cn_endpoint_id "987a0150-f855-4648-b771-fe0c7c7c1c2b"
48- #define cn_endpoint_id "coap_verify_code"
49- #define cn_app_server "119.3.184.255"
47+ #define cn_endpoint_id "lwm2m_002"
48+ #define cn_app_server "119.3.251.30"
5049#define cn_app_port "5684"
51- #define cn_psk_id "coap_verify_code"
52- unsigned char s_app_psk [] = {0x87 ,0x65 ,0x43 ,0x21 };
53-
50+ const unsigned char s_app_psk []= {0x01 ,0x02 ,0x03 ,0x04 ,0x05 ,0x06 };
5451
52+ #define cn_app_connectivity 0
53+ #define cn_app_lightstats 1
54+ #define cn_app_light 2
55+ #define cn_app_ledcmd 3
56+ #define cn_app_cmdreply 4
5557
56- #define cn_app_light 0
57- #define cn_app_ledcmd 1
58- #define cn_app_csq 2
5958#pragma pack(1)
6059typedef struct
6160{
6261 int8_t msgid ;
63- char intensity [5 ];
62+ int16_t rsrp ;
63+ int16_t ecl ;
64+ int16_t snr ;
65+ int32_t cellid ;
66+ }app_connectivity_t ;
67+
68+ typedef struct
69+ {
70+ int8_t msgid ;
71+ int16_t tog ;
72+ }app_toggle_t ;
73+
74+ typedef struct
75+ {
76+ int8_t msgid ;
77+ int16_t intensity ;
6478}app_light_intensity_t ;
6579
6680
6781typedef struct
6882{
6983 int8_t msgid ;
70- char led [3 ];
84+ uint16_t mid ;
85+ char led [3 ];
7186}app_led_cmd_t ;
7287
7388typedef struct
7489{
7590 int8_t msgid ;
76- char csq [3 ];
77- }app_net_csq_t ;
91+ uint16_t mid ;
92+ int8_t errorcode ;
93+ char curstats [3 ];
94+ }app_cmdreply_t ;
95+
7896#pragma pack()
7997
98+
99+
100+
80101//if your command is very fast,please use a queue here--TODO
81102#define cn_app_rcv_buf_len 128
82103static int s_rcv_buffer [cn_app_rcv_buf_len ];
83104static int s_rcv_datalen ;
84105static osal_semp_t s_rcv_sync ;
85106
107+ static void * s_lwm2m_handle = NULL ;
108+
109+
110+
86111//use this function to push all the message to the buffer
87112static int app_msg_deal (void * usr_data ,char * msg , int len )
88113{
89114 int ret = -1 ;
90115
91116 if (len <= cn_app_rcv_buf_len )
92117 {
118+ if (msg [0 ] == 0xaa && msg [1 ] == 0xaa )
119+ {
120+ printf ("OC respond message received! \n\r" );
121+ return ret ;
122+ }
93123 memcpy (s_rcv_buffer ,msg ,len );
94124 s_rcv_datalen = len ;
95125
@@ -105,21 +135,51 @@ static int app_msg_deal(void *usr_data,char *msg, int len)
105135static int app_cmd_task_entry ()
106136{
107137 int ret = -1 ;
108- int msgid ;
109138 app_led_cmd_t * led_cmd ;
139+ app_cmdreply_t replymsg ;
140+ int8_t msgid ;
110141
111142 while (1 )
112143 {
113144 if (osal_semp_pend (s_rcv_sync ,cn_osal_timeout_forever ))
114145 {
115- msgid = s_rcv_buffer [0 ];
116-
146+ msgid = s_rcv_buffer [0 ] & 0x000000FF ;
117147 switch (msgid )
118148 {
119149 case cn_app_ledcmd :
120150 led_cmd = (app_led_cmd_t * )s_rcv_buffer ;
121- printf ("LEDCMD:msgid:%d msg:%s \n\r" ,led_cmd -> msgid ,led_cmd -> led );
122- //if you need response,do it here--TODO
151+ printf ("LEDCMD:msgid:%d mid:%d msg:%s \n\r" ,led_cmd -> msgid ,ntohs (led_cmd -> mid ),led_cmd -> led );
152+ //add command action--TODO
153+ if (led_cmd -> led [0 ] == 'O' && led_cmd -> led [1 ] == 'N' )
154+ {
155+ //if you need response message,do it here--TODO
156+ replymsg .msgid = cn_app_cmdreply ;
157+ replymsg .mid = led_cmd -> mid ;
158+ printf ("reply mid is %d. \n\r" ,ntohs (replymsg .mid ));
159+ replymsg .errorcode = 0 ;
160+ replymsg .curstats [0 ] = 'O' ;
161+ replymsg .curstats [1 ] = 'N' ;
162+ replymsg .curstats [2 ] = ' ' ;
163+ oc_lwm2m_report (s_lwm2m_handle ,(char * )& replymsg ,sizeof (replymsg ),1000 ); ///< report cmd reply message
164+ }
165+
166+ else if (led_cmd -> led [0 ] == 'O' && led_cmd -> led [1 ] == 'F' && led_cmd -> led [2 ] == 'F' )
167+ {
168+
169+ //if you need response message,do it here--TODO
170+ replymsg .msgid = cn_app_cmdreply ;
171+ replymsg .mid = led_cmd -> mid ;
172+ printf ("reply mid is %d. \n\r" ,ntohs (replymsg .mid ));
173+ replymsg .errorcode = 0 ;
174+ replymsg .curstats [0 ] = 'O' ;
175+ replymsg .curstats [1 ] = 'F' ;
176+ replymsg .curstats [2 ] = 'F' ;
177+ oc_lwm2m_report (s_lwm2m_handle ,(char * )& replymsg ,sizeof (replymsg ),1000 ); ///< report cmd reply message
178+ }
179+ else
180+ {
181+
182+ }
123183 break ;
124184 default :
125185 break ;
@@ -130,59 +190,61 @@ static int app_cmd_task_entry()
130190 return ret ;
131191}
132192
193+
194+
133195static int app_report_task_entry ()
134196{
135197 int ret = -1 ;
198+ int lux = 0 ;
136199
137200 oc_config_param_t oc_param ;
138201 app_light_intensity_t light ;
139- app_net_csq_t csq ;
140- void * context ;
141202
142203 memset (& oc_param ,0 ,sizeof (oc_param ));
143204
144205 oc_param .app_server .ep_id = cn_endpoint_id ;
145- // oc_param.app_server.address = cn_app_server;
146- // oc_param.app_server.port = cn_app_port;
147- // oc_param.app_server.psk = (char *)s_app_psk;
148- // oc_param.app_server.psk_len = sizeof(s_app_psk);
149- // oc_param.app_server.psk_id = cn_psk_id;
206+ oc_param .app_server .psk = (char * )s_app_psk ;
207+ oc_param .app_server .psk_len = sizeof (s_app_psk );
208+ oc_param .app_server .psk_id = cn_endpoint_id ;
150209
151210 oc_param .boot_server .address = cn_app_server ;
152211 oc_param .boot_server .port = cn_app_port ;
153212 oc_param .boot_server .ep_id = cn_endpoint_id ;
154213 oc_param .boot_server .psk = (char * )s_app_psk ;
155214 oc_param .boot_server .psk_len = sizeof (s_app_psk );
156- oc_param .boot_server .psk_id = cn_psk_id ;
215+ oc_param .boot_server .psk_id = cn_endpoint_id ;
157216
158217 oc_param .boot_mode = en_oc_boot_strap_mode_client_initialize ;
159218 oc_param .rcv_func = app_msg_deal ;
160219
161- context = oc_lwm2m_config (& oc_param );
220+ s_lwm2m_handle = oc_lwm2m_config (& oc_param );
162221
163- if (NULL != context ) //success ,so we could receive and send
222+ if (NULL != s_lwm2m_handle ) //success ,so we could receive and send
164223 {
165224 //install a dealer for the led message received
166225 while (1 ) //--TODO ,you could add your own code here
167226 {
168- light .msgid = cn_app_light ;
169- memcpy (light .intensity ,"12345" ,5 );
170- oc_lwm2m_report (context ,(char * )& light ,sizeof (light ),1000 ); ///< report the light message
171- osal_task_sleep (10 * 1000 );
172227
173- csq .msgid = cn_app_csq ;
174- memcpy (csq .csq ,"012" ,3 );
175- oc_lwm2m_report (context ,(char * )& csq ,sizeof (csq ),1000 ); ///< report the light message
228+ lux ++ ;
229+ lux = lux %10000 ;
230+
231+ light .msgid = cn_app_light ;
232+ light .intensity = htons (lux );
233+ oc_lwm2m_report (s_lwm2m_handle ,(char * )& light ,sizeof (light ),1000 ); ///< report the light message
176234 osal_task_sleep (10 * 1000 );
177235 }
178236 }
179237
180238 return ret ;
181239}
182240
241+
242+
243+
183244int oc_lwm2m_demo_main ()
184245{
185246 osal_semp_create (& s_rcv_sync ,1 ,0 );
247+
186248 osal_task_create ("app_report" ,app_report_task_entry ,NULL ,0x1000 ,NULL ,2 );
187249 osal_task_create ("app_command" ,app_cmd_task_entry ,NULL ,0x1000 ,NULL ,3 );
188250
@@ -191,5 +253,3 @@ int oc_lwm2m_demo_main()
191253
192254
193255
194-
195-
0 commit comments