blob: 2fd96a85d7f06726b9f51e38473651332c79a2fc [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*************************************************************
2Description:
3 C file for L620 coap.
4Author:
5 LuoJian
6Date:
7 2019/3/25 9:23:29
8*************************************************************/
9#include <stdlib.h>
10#include <string.h>
11#include <stdio.h>
12#include <sys/types.h>
13#include <sys/socket.h>
14#include <linux/in.h>
15#include <unistd.h>
16
17#include "mbtk_coap.h"
18#include "mbtk_type.h"
19#include "mbtk_log.h"
20#include "mbtk_sock2.h"
21
22
23/*************************************************************
24 Variables:public
25*************************************************************/
26
27
28#define MBTK_ECOAP_TIMEOUT (15 * 1000)
29#define MBTK_ECOAP_WAIT_CLOSE_TIMEOUT (5 * 60 * 1000)
30#define MBTK_ECOAP_STACK_SIZE (12 * 1024) // task stack size
31
32#define ECOAPVER 1
33#define EOCAPTYPE 2
34#define ECOAPCODE 3
35#define ECOAPMSGID 4
36#define ECOAPTOKEN 5
37
38
39/*param--------------------*/
40// Point the first item.
41static mbtk_ecoap_package_s *mbtk_ecoap_head = NULL;
42static mbtk_ecoap_package_ver mbth_ecoap_package_ver={1,0,1,1};
43
44
45struct sockaddr_in serveraddr = {0};
46int socket_id;
47int addrlen;
48
49static mbtk_init_info *coap_init_info = NULL;
50static int coap_handle = -1;
51static bool coap_sock_inited = FALSE;
52static int coap_fd = -1;
53
54
55
56// Point the last item.
57static mbtk_ecoap_package_s *mbtk_ecoap_tail = NULL;
58static mbtk_ecoap_net_s mbtk_ecoap_net;
59static mbtk_ecoap_state_e mbtk_ecoap_state = MBTK_ECOAP_STATE_NON;
60
61//static uint16 mbtk_ecoap_curr_coap_id = 0;
b.liu9e8584b2024-11-06 19:21:28 +080062//static char mbtk_ecoap_host[MBTK_ECOAP_NEW_MAX+1] = {'\0'};
liubin281ac462023-07-19 14:22:54 +080063
b.liu9e8584b2024-11-06 19:21:28 +080064//static int mbtk_ecoap_port;
65//static int mbtk_ecoap_is_dtls;
liubin281ac462023-07-19 14:22:54 +080066static mbtk_ecoap_opt_2 mbtk_ecoap_option_2;
67static mbtk_ecoap_opt_3 mbtk_ecoap_option_3;
68static int mbtk_ecoap_recv = -1;
b.liu9e8584b2024-11-06 19:21:28 +080069//static int mbtk_ecoap_cur_time =0;
liubin281ac462023-07-19 14:22:54 +080070
71/*func------------------------*/
72void mbtk_ecoap_option_add(mbtk_ecoap_option_s *option);
73int mbtk_ecoap_list_del(uint16 message_id,int optDel);
b.liu9e8584b2024-11-06 19:21:28 +080074int mbtk_ecoap_get_message_type(void);
75void mbtk_ecoap_get_code(char *ptr);
liubin281ac462023-07-19 14:22:54 +080076
77#define HEXCHAR(x) ((x >= '0' && x <= '9') || (x >= 'a' && x <= 'f') || (x >= 'A' && x <= 'F'))
78
79
80#if 1
81
b.liu9e8584b2024-11-06 19:21:28 +080082#ifndef toupper
liubin281ac462023-07-19 14:22:54 +080083#define toupper(a) ((((a) >= 'a') && ((a) <= 'z')) ? ((a) - 'a' + 'A') : (a))
b.liu9e8584b2024-11-06 19:21:28 +080084#endif
liubin281ac462023-07-19 14:22:54 +080085
86void HexToStr(byte *pbDest, byte *pbSrc, int nLen)
87{
88 char ddl,ddh;
89 int i;
90
91 for (i=0; i<nLen; i++)
92 {
93 ddh = 48 + pbSrc[i] / 16;
94 ddl = 48 + pbSrc[i] % 16;
95 if (ddh > 57)
96 {
97 ddh = ddh + 7;
98 }
99 if (ddl > 57)
100 {
101 ddl = ddl + 7;
102 }
103 pbDest[i*2] = ddh;
104 pbDest[i*2+1] = ddl;
105 }
106 pbDest[nLen*2] = '\0';
107}
108
109uint8_t StrToHex(byte *pbDest, byte *pbSrc, int nLen)
110{
111 char h1,h2;
112 byte s1,s2;
113 uint8_t i;
114
115 for (i=0; i<nLen; i++)
116 {
117 h1 = pbSrc[2*i];
118 h2 = pbSrc[2*i+1];
119 s1 = toupper(h1) - 0x30;
120 if (s1 > 9)
121 {
122 s1 -= 7;
123 }
124 s2 = toupper(h2) - 0x30;
125 if (s2 > 9)
126 {
127 s2 -= 7;
128 }
129 pbDest[i] = s1*16 + s2;
130 //DS_MBTK_MSG1_HIGH("StrToHex:%.2X",pbDest[i]);
131 }
132 return i;
133}
134
135void coap_log(int is_ascii,const char* data,int len)
136{
137 if(len <= 0) {
138 return;
139 }
140
141 if(is_ascii) { // Is ASCII
142 int i = 0;
143 int count = len / 100 + 1;
144
145 // Has '\0'
146 if(len != strlen(data)) {
147 return;
148 }
149
150 while(i < count) {
151 printf("%s\n",data + i * 100);
152 i++;
153 }
154 } else { // Is hex
155 int index = 0;
156 int i = 0;
157 char buf_temp[100];
158 while(index % 16 == 0 && index < len) {
159 memset(buf_temp,0,100);
160 for(i = 0;i < 16 && index + i < len;i++) {
161 snprintf(buf_temp + i * 3,100 - i,"%.2x ",((char*)data)[index + i]);
162 }
163 index += 16;
164 printf("%s\n", buf_temp);
165 }
166 }
167}
168
169
170
171static void
172mbtk_ecoap_show_error
173(
174 int rsp_errno
175)
176{
177 char temp[50] = {0};
178 memset(temp, 0, 50);
179 snprintf(temp,50,"\r\n+MCOAPERROR:%d\r\n",rsp_errno);
180 printf("%s\n",temp);
181}
182
183int ecoap_recv_buffer(char * dest,const char* data,int len)
184{
185 if( len <= 0) {
186 return -1;
187 }
188 int buffer_len = 0;
189 char Payload[MBTK_ECOAP_DATA_MAX*2+1] ={'\0'};
190 int pud_lenth = mbtk_coap_get_pdu_Length();
191 int type = mbtk_ecoap_get_message_type();
192 char code[12];
193 memset(code,0x00,sizeof(code));
194 mbtk_ecoap_get_code(code);
195 uint16_t mesageId = coapGetRecvMessageID();
196 memset(Payload,0x00,MBTK_ECOAP_DATA_MAX*2+1);
197 snprintf(dest,MBTK_ECOAP_DATA_MAX,
198 "+COAPNMI:%d\r\nMessage Type:%d\r\nCode:\"%s\"\r\nMessage ID:%d\r\nPayload:",
199 pud_lenth,
200 type,
201 code,
b.liubcf86c92024-08-19 19:48:28 +0800202 mesageId);
liubin281ac462023-07-19 14:22:54 +0800203
204
205 int i = 0;
206 for(i = 0; i < len; i++)
207 {
208 if(data[i] == 0xFF && i> 3) //BUG 46397
209 {
210 LOGE("data[i] == 0xFF\n");
211 memcpy(Payload, data+i+1, len - i-1);
212 Payload[len-i -1]='\0';
213 break;
214 }
215 }
216
217 LOGE("len:%d, i :%d\n",len,i);
218 if(Payload[len-i -1] == '\0' )
219 {
b.liubcf86c92024-08-19 19:48:28 +0800220 LOGE("==\0-------------------\n");
liubin281ac462023-07-19 14:22:54 +0800221 // coap_log(-1,(char*)Payload,len - i-1);
222 }else{
223 LOGE("!=\0--------------------\n");
224 Payload[0]='\0';
225 }
226
b.liubcf86c92024-08-19 19:48:28 +0800227 buffer_len = strlen(dest) + strlen(Payload);
liubin281ac462023-07-19 14:22:54 +0800228 strcat(dest,Payload);
229 LOGE("2---------buffer_len:%d,coap_recv_buffer_dest:%s,Payload:%s\n",buffer_len,dest,Payload);
230 return buffer_len;
231}
232
233int mbtk_ecoap_get_message_type(void)
234{
235 switch(coapGetRecvType())
236 {
237 case COAP_CONFIRMABLE:
238 return 0;
239 break;
240
241 case COAP_NON_CONFIRMABLE:
242 return 1;
243 break;
244
245 case COAP_ACKNOWLEDGEMENT:
246 return 2;
247 break;
248
249 case COAP_RESET:
250 return 3;
251 break;
252 }
253 return 0;
254}
255
256void mbtk_ecoap_get_code(char *ptr)
257{
258 switch(coapGetRecvCode()) {
259 case COAP_EMPTY:
260 memcpy(ptr,"0.00",4);
261 break;
262 case COAP_GET:
263 memcpy(ptr,"0.01",4);
264 break;
265 case COAP_POST:
266 memcpy(ptr,"0.02",4);
267 break;
268 case COAP_PUT:
269 memcpy(ptr,"0.03",4);
270 break;
271 case COAP_DELETE:
272 memcpy(ptr,"0.04",4);
273 break;
274 case COAP_CREATED:
275 memcpy(ptr,"2.01",4);
276 break;
277 case COAP_DELETED:
278 memcpy(ptr,"2.02",4);
279 break;
280 case COAP_VALID:
281 memcpy(ptr,"2.03",4);
282 break;
283 case COAP_CHANGED:
284 memcpy(ptr,"2.04",4);
285 break;
286 case COAP_CONTENT:
287 memcpy(ptr,"2.05",4);
288 break;
289 case COAP_CONTINUE:
290 memcpy(ptr,"2.31",4);
291 break;
292 case COAP_BAD_REQUEST:
293 memcpy(ptr,"4.00",4);
294 break;
295 case COAP_UNAUTHORIZED:
296 memcpy(ptr,"4.01",4);
297 break;
298 case COAP_BAD_OPTION:
299 memcpy(ptr,"4.02",4);
300 break;
301 case COAP_FORBIDDEN:
302 memcpy(ptr,"4.03",4);
303 break;
304 case COAP_NOT_FOUND:
305 memcpy(ptr,"4.04",4);
306 break;
307 case COAP_METHOD_NOT_ALLOWED:
308 memcpy(ptr,"4.05",4);
309 break;
310 case COAP_NOT_ACCEPTABLE:
311 memcpy(ptr,"4.06",4);
312 break;
313 case COAP_PRECONDITION_FAILED:
314 memcpy(ptr,"4.12",4);
315 break;
316 case COAP_REQUEST_ENTITY_TOO_LARGE:
317 memcpy(ptr,"4.13",4);
318 break;
319 case COAP_UNSUPPORTED_CONTENT_FORMAT:
320 memcpy(ptr,"4.15",4);
321 break;
322 case COAP_INTERNAL_SERVER_ERROR:
323 memcpy(ptr,"5.00 ",4);
324 break;
325 case COAP_NOT_IMPLEMENTED:
326 memcpy(ptr,"5.01",4);
327 break;
328 case COAP_BAD_GATEWAY:
329 memcpy(ptr,"5.02",4);
330 break;
331 case COAP_SERVICE_UNAVAILABLE:
332 memcpy(ptr,"5.03",4);
333 break;
334 case COAP_GATEWAY_TIMEOUT:
335 memcpy(ptr,"5.04",4);
336 break;
337 case COAP_PROXYING_NOT_SUPPORTED:
338 memcpy(ptr,"5.05",4);
339 break;
340 default:
b.liubcf86c92024-08-19 19:48:28 +0800341 sprintf(ptr, "UndefCod%u",(unsigned)(coapGetRecvCode()));
liubin281ac462023-07-19 14:22:54 +0800342 }
343}
344
b.liu9e8584b2024-11-06 19:21:28 +0800345#if 0
liubin281ac462023-07-19 14:22:54 +0800346static int
347mbtk_ecoap_is_open()
348{
349 if(mbtk_ecoap_state >= MBTK_ECOAP_STATE_OPENED)
350 return 0;
351
352 return -1;
353}
354
355
356static void mbtk_ecoap_reset()
357{
358 memset(&mbtk_ecoap_net,0x0,sizeof(mbtk_ecoap_net_s));
359 mbtk_ecoap_net.message_id = 1; //change mbtk_ecoap_net.message_id = 0;
360 memset(&mbtk_ecoap_option_2, 0x0, sizeof(mbtk_ecoap_opt_2));
361 memset(&mbth_ecoap_package_ver, 0x0,sizeof(mbtk_ecoap_package_ver));
362 mbth_ecoap_package_ver.version = 1;
363 mbth_ecoap_package_ver.type = 0;
364 mbth_ecoap_package_ver.code = 1;
365 mbth_ecoap_package_ver.message_id = 1;
366 mbtk_ecoap_recv = -1;
367
368 mbtk_ecoap_package_s *coap = mbtk_ecoap_head;
369 mbtk_ecoap_package_s *coap_next = NULL;
370 mbtk_ecoap_option_s * option_ptr = NULL;
371 mbtk_ecoap_option_s * option_ptr_next = NULL;
b.liubcf86c92024-08-19 19:48:28 +0800372
liubin281ac462023-07-19 14:22:54 +0800373 while(coap != NULL)
374 {
375 coap_next = coap->next;
376 if(coap->payload != NULL)
377 free(coap->payload);
378
379 if(coap->options != NULL)
380 {
381 option_ptr = coap->options;
382 while(option_ptr != NULL)
383 {
384 option_ptr_next = option_ptr->next;
385 free(option_ptr);
386 option_ptr = NULL;
387 option_ptr = option_ptr_next;
388 }
b.liubcf86c92024-08-19 19:48:28 +0800389
liubin281ac462023-07-19 14:22:54 +0800390 }
b.liubcf86c92024-08-19 19:48:28 +0800391
liubin281ac462023-07-19 14:22:54 +0800392 free(coap);
393 coap = NULL;
394 coap = coap_next;
395
396 }
397
398 mbtk_ecoap_head = mbtk_ecoap_tail = NULL;
399}
400
401
402static void mbtk_ecoap_close()
403{
404
405}
b.liu9e8584b2024-11-06 19:21:28 +0800406#endif
liubin281ac462023-07-19 14:22:54 +0800407
408
409
410
411mbtk_ecoap_package_s *mbtk_ecoap_get_by_msg_id(uint16 message_id)
412{
413 if(mbtk_ecoap_head == NULL)
414 {
415 }
416 else
417 {
418 mbtk_ecoap_package_s *coap_ptr = mbtk_ecoap_head;
419 while(coap_ptr != NULL)
420 {
421 if(coap_ptr->message_id == message_id)
422 {
423 return coap_ptr;;
424 }
425 coap_ptr = coap_ptr->next;
426 }
427 }
428 return NULL;
429}
430
431static void mbtk_ecoap_list_item_init(unsigned int sig,mbtk_ecoap_package_s *coap,uint16 message_id)
432{
433 switch(sig)
434 {
435 case ECOAPVER:
436 coap->version = message_id;
437 break;
438 case EOCAPTYPE:
439 coap->type = message_id;
440 break;
441 case ECOAPCODE:
442 coap->code = message_id;
443 break;
444 case ECOAPMSGID:
445 coap->message_id = message_id;
446 break;
447 case ECOAPTOKEN:
448 coap->token_len = message_id;
449 break;
450 default:
451 break;
452 }
453 coap->next = NULL; //change delete this code
454}
455
456
457mbtk_ecoap_package_s* mbtk_ecoap_list_add(uint16 message_id)
458{
459 LOGE("mbtk_ecoap_list_add-----memssage_id:%d\n",message_id);
460 if(mbtk_ecoap_head == NULL)
461 {
462 mbtk_ecoap_head = (mbtk_ecoap_package_s*)malloc(sizeof(mbtk_ecoap_package_s));
463 memset(mbtk_ecoap_head, 0,sizeof(mbtk_ecoap_package_s));
464 mbtk_ecoap_list_item_init(ECOAPMSGID,mbtk_ecoap_head,message_id);
465 mbtk_ecoap_tail = mbtk_ecoap_head;
466 return mbtk_ecoap_head;
467 }
468 else
469 {
470 mbtk_ecoap_package_s *coap_ptr = mbtk_ecoap_head;
471 while(coap_ptr != NULL)
472 {
473 if(coap_ptr->message_id == message_id)
474 {
475 printf("Coap %d exist.\n",message_id);
476 return NULL;
477 }
478 coap_ptr = coap_ptr->next;
479 }
480 mbtk_ecoap_tail->next = (mbtk_ecoap_package_s*)malloc(sizeof(mbtk_ecoap_package_s));
481 mbtk_ecoap_list_item_init(ECOAPMSGID,mbtk_ecoap_tail->next,message_id);
482 mbtk_ecoap_tail = mbtk_ecoap_tail->next;
483 return mbtk_ecoap_tail;
484 }
485}
486
487static void mbtk_ecoap_free(mbtk_ecoap_package_s *coap)
488{
489 if(coap != NULL)
490 {
491 if(coap->payload != NULL)
492 free(coap->payload);
493
494 if(coap->options != NULL)
495 {
496 mbtk_ecoap_option_s *option_ptr = coap->options;
497 while(option_ptr != NULL)
498 {
499 free(option_ptr);
500 option_ptr = option_ptr->next;
501 }
502 }
503
504 free(coap);
505 coap = NULL;
506 }
507}
508
509int mbtk_ecoap_list_del(uint16 message_id,int optDel)
510{
511 LOGE("mbtk_ecoap_list_del:message_id:%d\n",message_id);
512 if(mbtk_ecoap_head == NULL)
513 {
514 printf("mbtk_ecoap_head == NULL\n");
515 return -1;
516 }
517
518 mbtk_ecoap_recv = -1;
519 if(optDel)
520 {
521 memset(&mbtk_ecoap_option_2, 0x0, sizeof(mbtk_ecoap_opt_2));
522 memset(&mbtk_ecoap_option_3, 0x0, sizeof(mbtk_ecoap_opt_3));
523 memset(&mbth_ecoap_package_ver,0x0,sizeof(mbth_ecoap_package_ver));
524 mbth_ecoap_package_ver.version = 1;
525 mbth_ecoap_package_ver.type = 0;
526 mbth_ecoap_package_ver.code = 1;
527 mbth_ecoap_package_ver.message_id = 1;
528 }
529 mbtk_ecoap_package_s *coap = mbtk_ecoap_head;
530 if(mbtk_ecoap_head->message_id == message_id) // Is first item.
531 {
532 mbtk_ecoap_head = mbtk_ecoap_head->next;
533
534 mbtk_ecoap_free(coap);
535 return 0;
536 }
537 else // Not first item.
538 {
b.liubcf86c92024-08-19 19:48:28 +0800539 while(coap->next != NULL)
liubin281ac462023-07-19 14:22:54 +0800540 {
541 if(message_id == coap->next->message_id) // delete
542 {
543 mbtk_ecoap_package_s *coap_tmp = coap->next;
544 if(mbtk_ecoap_tail == coap_tmp)
545 {
546 mbtk_ecoap_tail = coap;
547 }
548 coap->next = coap_tmp->next;
549 mbtk_ecoap_free(coap_tmp);
550 return 0;;
551 }
552 coap = coap->next;
553 }
554 return -1;
555 }
556}
557
558void mbtk_ecoap_option_add(mbtk_ecoap_option_s *option)
559{
560 if(option != NULL)
561 {
562 switch(option->type)
563 {
564 case COAP_OPTION_IF_MATCH:
565 case COAP_OPTION_URI_HOST:
566 case COAP_OPTION_ETAG:
567 case COAP_OPTION_IF_NONE_MATCH:
568 case COAP_OPTION_LOCATION_PATH:
569 case COAP_OPTION_LOCATION_QUERY:
570 case COAP_OPTION_PROXY_URI:
571 case COAP_OPTION_PROXY_SCHEME:
572 case COAP_OPTION_URI_PORT:
573 case COAP_OPTION_ACCEPT:
574 case COAP_OPTION_SIZE2:
575 case COAP_OPTION_SIZE1:
576 {
577 break;
578 }
579 case COAP_OPTION_BLOCK2:
580 case COAP_OPTION_BLOCK1:
581 {
582 LOGE("option->opt.opt_block.number = %d\n",option->opt.opt_block.number);
583 if(option->opt.opt_block.number <= 0x0F)
584 {
585 uint8_t temp[1];
586 temp[0] = (uint8_t)(((option->opt.opt_block.number << 4) & 0xF0)
587 | ((option->opt.opt_block.more_flag << 3) & 0x08)
588 | (option->opt.opt_block.size & 0x07));
589 coapAddOption(option->type,1,temp);
590 }
591 else if(option->opt.opt_block.number <= 0x0FFF)
592 {
593 uint8_t temp[2];
594 temp[0] = (uint8_t)(option->opt.opt_block.number >> 4);
595 temp[1] = (uint8_t)(((option->opt.opt_block.number << 4) & 0xF0)
596 | ((option->opt.opt_block.more_flag << 3) & 0x08)
597 | (option->opt.opt_block.size & 0x07));
598 coapAddOption(option->type,2,temp);
599
600 }
601 else
602 {
603 uint8_t temp[3];
604 temp[0] = (uint8_t)(option->opt.opt_block.number >> 12);
605 temp[1] = (uint8_t)(option->opt.opt_block.number >> 4);
606 temp[2] = (uint8_t)(((option->opt.opt_block.number << 4) & 0xF0)
607 | ((option->opt.opt_block.more_flag << 3) & 0x08)
608 | (option->opt.opt_block.size & 0x07));
609 coapAddOption(option->type,3,temp);
610
611 }
612 break;
613 }
614 case COAP_OPTION_MAX_AGE:
615 {
616 uint8_t temp[1];
617 temp[0] = (uint8_t)option->opt.opt_int;
618 coapAddOption(option->type,1,temp);
619 break;
620 }
621 case COAP_OPTION_OBSERVE:
622 {
623 uint8_t temp[1];
624 temp[0] = (uint8_t)option->opt.opt_int;
625 coapAddOption(option->type,1,temp);
626 break;
627 }
628 case COAP_OPTION_URI_QUERY:
629 {
630 coapAddURIQuery((char*)option->opt.opt_str);
631 break;
632 }
633 case COAP_OPTION_URI_PATH:
634 {
635 coapSetURI((char*)option->opt.opt_str);
636 break;
637 }
638 case COAP_OPTION_CONTENT_FORMAT:
639 {
640 coapSetContentFormat(option->opt.opt_content_format);
641 break;
642 }
643 default:
b.liu9e8584b2024-11-06 19:21:28 +0800644 printf("No such type:%d\n",option->type);
liubin281ac462023-07-19 14:22:54 +0800645 break;
646 }
647 }
648}
649
650static void mbtk_ecoap_set_option(mbtk_ecoap_package_s *coap,
651 mbtk_coap_option_type type,void *str,
652 int int0,int int1,int int2)
653{
654 if(coap != NULL)
655 {
656/* if(coap->options == NULL)
657 {
658 coap->options = (mbtk_ecoap_option_s*)malloc(sizeof(mbtk_ecoap_option_s));
659 memset(coap->options,0x0,sizeof(mbtk_ecoap_option_s));
660 coap->options->type = type;
661 coap->options->next = NULL;
662 }
663*/
664 mbtk_ecoap_option_s *option = coap->options;
665 while(option != NULL)
666 {
667 LOGE("1option != NULL\n");
668 if(option->type == type)
669 {
670 break;
671 }
672 option = option->next;
673 }
674
675 LOGE("----------------------\n");
676 if(option == NULL) // Now option
677 {
678 LOGE("option == NULL\n");
679 option = (mbtk_ecoap_option_s*)malloc(sizeof(mbtk_ecoap_option_s));
680 memset(option,0x0,sizeof(mbtk_ecoap_option_s));
681// option->type = type;
682
683 option->next = coap->options;
684 coap->options = option;
685 option->type = type;
686 }
687 else // Change option
688 {
689 // Do noting.
690 }
691
692 switch(type)
693 {
694 case COAP_OPTION_IF_MATCH:
695 case COAP_OPTION_URI_HOST:
696 case COAP_OPTION_ETAG:
697 case COAP_OPTION_IF_NONE_MATCH:
698 case COAP_OPTION_LOCATION_PATH:
699 case COAP_OPTION_URI_QUERY:
700 case COAP_OPTION_LOCATION_QUERY:
701 case COAP_OPTION_PROXY_URI:
702 case COAP_OPTION_PROXY_SCHEME:
703 case COAP_OPTION_URI_PATH:
704 {
705 memset(option->opt.opt_str,0x0,128);
706 memcpy(option->opt.opt_str,str,strlen((char*)str));
707 break;
708 }
709 case COAP_OPTION_OBSERVE:
710 case COAP_OPTION_MAX_AGE:
711 case COAP_OPTION_URI_PORT:
712 case COAP_OPTION_SIZE2:
713 case COAP_OPTION_SIZE1:
714 {
715 option->opt.opt_int = (int)int0;
716 break;
717 }
718 case COAP_OPTION_BLOCK2:
719 case COAP_OPTION_BLOCK1:
720 {
721 option->opt.opt_block.size = (mbtk_ecoap_option_block_e)int0;
722 option->opt.opt_block.number = (int)int1;
723 option->opt.opt_block.more_flag = (uint8)int2;
724 break;
725 }
726 case COAP_OPTION_CONTENT_FORMAT:
727 {
728 option->opt.opt_content_format = (mbtk_content_format_type)int0;
729 break;
730 }
731 case COAP_OPTION_ACCEPT:
732 {
733 option->opt.opt_content_format = (mbtk_content_format_type)int0;
734 break;
735 }
736 default:
b.liu9e8584b2024-11-06 19:21:28 +0800737 printf("No such type:%d\n",type);
liubin281ac462023-07-19 14:22:54 +0800738 break;
739 }
740 }
741}
742
743
744static void mbtk_ecoap_send_ack(mbtk_coap_code_type code,
745 uint16_t message_id,uint8 token_len,uint8 *token)
746{
747 coapReset();
748 coapSetVersion(1);
749 coapSetType(COAP_ACKNOWLEDGEMENT);
750 coapSetMessageID(message_id);
751
752 if(code == COAP_POST || code == COAP_PUT)
753 coapSetCode(COAP_CHANGED);
754 else
755 coapSetCode(COAP_EMPTY);
756
757 if(token_len > 0)
758 {
759 coapSetToken(token, token_len);
760 }
761
762 uint8_t* pduPointer = coapGetPDUPointer();
763 int pduLen = coapGetPDULength();
764 int err;
765 int res = mbtk_sock_write(coap_handle, coap_fd, pduPointer, pduLen, 300, &err);
766 if(res != pduLen)
767 {
768 printf("Send ACK fail.[res:%d,pduLen:%d],err:%d\n",res,pduLen,err);
769 }
770 else
771 {
772 printf("Send ACK to server success.\n");
773 }
774}
775
776
777
778static int mbtk_ecoap_handle_sock_init(char *ip_addr, int port, bool is_support_ssl, bool ingnore_cert)
779{
780 mbtk_sock_info *sock_info = NULL;
781 int rc = 0;
782 int errno;
b.liubcf86c92024-08-19 19:48:28 +0800783
liubin281ac462023-07-19 14:22:54 +0800784 sock_info = (mbtk_sock_info *)malloc(sizeof(mbtk_sock_info));
785 if(sock_info == NULL)
786 {
787 rc = -1;
788 return rc;
789 }
790 memcpy(sock_info->address, ip_addr, strlen(ip_addr));
791 sock_info->port = port;
792 sock_info->is_support_ssl = is_support_ssl;
793 sock_info->ingnore_cert = ingnore_cert;
794 sock_info->type = MBTK_SOCK_UDP;
b.liubcf86c92024-08-19 19:48:28 +0800795
liubin281ac462023-07-19 14:22:54 +0800796 printf("host %s\nport %d\nis_support_ssl %d\ningnore_cert %d\n",sock_info->address,sock_info->port,sock_info->is_support_ssl,sock_info->ingnore_cert);
797
798 if(!coap_sock_inited)
799 {
800 coap_handle = mbtk_sock_init(coap_init_info);
801 if (coap_handle < 0 )
802 {
803 rc = -1;
804 return rc;
805 }
806 coap_sock_inited = TRUE;
807 }
808
809
810 coap_fd = mbtk_sock_open(coap_handle, sock_info, 3000, &errno);
811 if(coap_fd < 0)
812 {
813 printf("creat socket fail, errno:%d\n",errno);
814 rc = -1;
815 return rc;
816 }
817 mbtk_ecoap_state = MBTK_ECOAP_STATE_READY;
818 return rc;
819}
820
821
822
823int mbtk_ecoap_ecoap_send(mbtk_ecoap_package_s* coap)
824{
825 if(coap != NULL)
826 {
827 coapReset();
828 coapSetVersion(coap->version);
829 coapSetType(coap->type);
830 coapSetCode(coap->code);
831 coapSetMessageID(coap->message_id);
832
833 if(coap->token_len > 0)
834 {
835 coapSetToken(coap->token, coap->token_len);
836 }
837
838 mbtk_ecoap_option_s *option = coap->options;
839 while(option != NULL)
840 {
841 mbtk_ecoap_option_add(option);
842 option = option->next;
843 }
844
845 if(coap->payload_len > 0)
846 {
847 coapSetPayload((uint8_t*)coap->payload, coap->payload_len);
848 }
849
850 uint8_t *pduPointer = coapGetPDUPointer();
851 int pduLen = coapGetPDULength();
852 uint8_t pduPointer2[MBTK_ECOAP_DATA_MAX*2];
853 int err;
854 memset ( pduPointer2,0x00,MBTK_ECOAP_DATA_MAX*2 );
855 if((mbtk_ecoap_option_2.optlen + pduLen) > MBTK_ECOAP_DATA_MAX*2)
856 {
857 printf("This length is too long\n");
858 return -1;
859 }
860
861 LOGE("pduLen:%d, optlen:%d\n",pduLen ,mbtk_ecoap_option_2.optlen);
862 // printf("pduPointer:\n");
863 // coap_log(-1,(char*)pduPointer,pduLen);
864 int j=0,k=0;
865 LOGE("pduPointer-----------end\n");
866// int optflag = 0;
867 pduPointer2[0]= *pduPointer++;
868 pduPointer2[1]= *pduPointer++;
869 pduPointer2[2]= *pduPointer++;
870 pduPointer2[3]= *pduPointer++;
871 for(j = 0; j < mbtk_ecoap_option_2.optlen; j++)
872 {
873 pduPointer2[4+j] = mbtk_ecoap_option_2.optVal[j];
874 LOGE("j:%d, %X",j, mbtk_ecoap_option_2.optVal[j]);
875 k++;
876 }
877 int pduLen2 = 4;
878 while(pduLen > pduLen2 )
879 {
880 LOGE("\r\npduLen > pduLen2 \r\n");
881 LOGE("pduLen:%d,pduPointer:%X,k:%d\n",pduLen2,*pduPointer,k);
882 pduPointer2[pduLen2+k] = *pduPointer++;
883 pduLen2++;
884 }
885
886 pduLen += mbtk_ecoap_option_2.optlen;
887
888 printf("sendpdu:--------pduLen:%d\n",pduLen);
889 coap_log(FALSE,(char*)pduPointer2,pduLen);
890 int send_len = 0;
891
892 if((send_len = mbtk_sock_write(coap_handle, coap_fd, pduPointer2, pduLen, 300, &err)) != pduLen)
893// if((send_len = sendto(socket_id, pduPointer2, pduLen, 0, (struct sockaddr*)&serveraddr, addrlen)) != pduLen)
894 {
895 printf("coap_udp_sendto complate.[%d/%d]\n",send_len,pduLen);
896
897 coap->send_count++;
898 return -1;
899 }
900
901 if(coap->type != COAP_CONFIRMABLE)
902 {
903 mbtk_ecoap_list_del(coap->message_id,0);
904 }
905
906 }
907 return 0;
908}
909
910void mbtk_ecoap_recv_buffer(char * dest)
911{
912 int pud_lenth = mbtk_coap_get_pdu_Length();
913 int type = mbtk_ecoap_get_message_type();
914 char code[12];
915 memset(code,0x00,sizeof(code));
916 mbtk_ecoap_get_code(code);
917 uint16_t mesageId = coapGetRecvMessageID();
918 memset(dest,0x00,129);
919 snprintf(dest,128,
920 "+COAPNMI:%d\r\nMessage Type:%d\r\nCode:\"%s\"\r\nMessage ID:%d\r\nPayload:",
921 pud_lenth,
922 type,
923 code,
b.liubcf86c92024-08-19 19:48:28 +0800924 mesageId);
liubin281ac462023-07-19 14:22:54 +0800925}
926
927
928static void mbtk_ecoap_recv_resolve(const void *data,uint16 data_len)
929{
930 if(coapCreateRecv((uint8_t*)data, data_len) <= 0)
931 {
932 printf("coapCreateRecv fail.\n");
933 }
934 else
935 {
936 LOGE("coapCreateRecv() success.\n");
937
938 uint8_t version = coapGetRecvVersion();
939 mbtk_coap_type type = coapGetRecvType();
940 mbtk_coap_code_type code = coapGetRecvCode();
941 uint16_t message_id = coapGetRecvMessageID();
942 int pud_lenth = mbtk_coap_get_pdu_Length();
943 LOGE("version:%d;type:%d\n",version,type);
944 LOGE("code:%d;message_id:%d,pud_lenth:%d\n",code,message_id,pud_lenth);
945
946 uint8 token_len = coapGetRecvTokenLength();
947 char token[9] = {0};
948 memcpy(token,coapGetRecvTokenPointer(),token_len);
949 LOGE("token[%d]:%s\n",token_len,token);
950
b.liu9e8584b2024-11-06 19:21:28 +0800951// uint16 playload_len = coapGetRecvPayloadLength();
952// uint8 *playload = coapGetRecvPayloadPointer();
liubin281ac462023-07-19 14:22:54 +0800953// printf("----------------payload-----------------\n");
954// coap_log(0,(char*)playload,playload_len);
955// printf("--------------payload end----------------\n");
956
957
958 char buff_data[MBTK_ECOAP_DATA_MAX*2+1];
959 memset ( buff_data,0x00,MBTK_ECOAP_DATA_MAX*2+1 );
960 int buffer_len = ecoap_recv_buffer(buff_data, data, data_len);
961 printf("recv buff_len:%d\n",buffer_len);
962 printf("buf:\n%s\r\n",buff_data);
b.liubcf86c92024-08-19 19:48:28 +0800963
liubin281ac462023-07-19 14:22:54 +0800964 LOGE("type:%X\n",type);
965 if(type == COAP_CONFIRMABLE) // Should ACK
966 {
967 mbtk_ecoap_send_ack(code,message_id,token_len,(uint8*)token);
968 }
969 else if(type == COAP_NON_CONFIRMABLE)
970 {
971 // Do nothing.
972 }
973 else if(type == COAP_ACKNOWLEDGEMENT)
974 {
975 mbtk_ecoap_package_s * coap_send = mbtk_ecoap_get_by_msg_id(message_id);
976 if(coap_send != NULL)
977 {
b.liu9e8584b2024-11-06 19:21:28 +0800978 uint32_t number;
liubin281ac462023-07-19 14:22:54 +0800979 uint8_t more_flag;
980 uint8_t size;
981 LOGE("Get message(%d) ack.\n",message_id);
982 if(coapGetRecvOptionBlock2(&number, &more_flag, &size))
983 {
984 LOGE("Block2 (size:%d; more_flag:%d; number:%d)\n",size,more_flag,number);
985 if(more_flag) // Has data no read.
986 {
987 coap_send->message_id = ++mbtk_ecoap_net.message_id ;
988 mbtk_ecoap_set_option(coap_send, COAP_OPTION_BLOCK2, NULL,
989 size,number + 1,0);
990
991 mbtk_ecoap_ecoap_send(coap_send);
992 }
993 else
994 {
995 mbtk_ecoap_list_del(message_id,0);
996 }
997 }
998 else
999 {
1000 printf("coapGetRecvOptionBlock2() fail.\n");
1001 mbtk_ecoap_list_del(message_id,0);
1002 }
1003 }
1004 else
1005 {
1006 printf("Coap not match.\n");
1007 }
1008 }
1009 else // COAP_RESET
1010 {
1011 printf("Coap type error.\n");
1012 }
1013 }
1014 coapDeleteRecv();
1015
1016}
1017
1018
1019static int ecoap_sig_recv_handle(void)
1020{
1021 LOGE("coap_sig_recv_handle----------start\n");
1022
1023 uint8_t buffer[MBTK_ECOAP_DATA_MAX*2+1];
1024 memset ( buffer,0x00,MBTK_ECOAP_DATA_MAX*2+1 );
1025 int ret=-1;
1026 int err = -1;
1027 while(TRUE)
1028 {
1029 ret = mbtk_sock_read(coap_handle, coap_fd, buffer, MBTK_ECOAP_DATA_MAX*2, 1000*10, &err);
1030 if(ret <= 0)
1031 {
1032 printf("read fail, err:%d",err);
1033 break;
1034 }
1035 printf("recv_from ret:%d\n",ret);
1036 coap_log(FALSE,(char*)buffer,ret);
1037 mbtk_ecoap_recv_resolve(buffer,ret);
1038 }
1039
1040 return ret;
1041}
1042
1043static int ecoap_sig_send_complete_handle(void )
1044{
1045 int ret=-1;
1046 mbtk_ecoap_package_s *coap = mbtk_ecoap_get_by_msg_id(mbtk_ecoap_net.message_id);
1047 if(coap == NULL)
1048 {
1049 mbtk_ecoap_show_error(MBTK_ECOAP_ERR_MSG_ID);
1050 return -1;
1051 }
1052
1053 ret = mbtk_ecoap_ecoap_send(coap);
1054
1055 return ret;
1056
1057}
1058
b.liu9e8584b2024-11-06 19:21:28 +08001059//static int mbtk_ecoap_open(int open);
liubin281ac462023-07-19 14:22:54 +08001060
1061
1062
1063#endif
1064
1065
1066/*************************************************************
1067 Public Function Definitions
1068*************************************************************/
1069
1070/*************************************************************
1071 ip_addr: url
1072 port
1073
1074*************************************************************/
1075int mbtk_coap_ecoapnew_exec_cmd
1076(
1077 char *ip_addr,
1078 int port,
b.liubcf86c92024-08-19 19:48:28 +08001079 bool is_support_ssl,
liubin281ac462023-07-19 14:22:54 +08001080 bool ingnore_cert
1081)
1082{
1083 printf("mbtk_coap_ecoapnew_exec_cmd()------start1\n");
1084 int ret = 0;
1085 if( !ip_addr)
1086 {
1087 mbtk_ecoap_show_error(MBTK_ECOAP_ERR_NO_READY);
1088 return -1;
1089 }
1090
1091 ret = mbtk_ecoap_handle_sock_init(ip_addr, port, is_support_ssl, ingnore_cert);
b.liubcf86c92024-08-19 19:48:28 +08001092
liubin281ac462023-07-19 14:22:54 +08001093 return ret;
1094}
1095
1096int mbtk_coap_ecoaprxmod_exec_cmd
1097(
1098 int mode
1099)
1100{
1101 int result = 0;
b.liu9e8584b2024-11-06 19:21:28 +08001102// int rx_mod = mode;
b.liubcf86c92024-08-19 19:48:28 +08001103
liubin281ac462023-07-19 14:22:54 +08001104 return result;
1105}
1106
1107int mbtk_coap_ecoappr_exec_cmd
1108(
1109 int format
1110)
1111{
1112 int result = 0;
b.liu9e8584b2024-11-06 19:21:28 +08001113// uint16 ecoappr = format;
liubin281ac462023-07-19 14:22:54 +08001114 return result;
1115}
1116
1117int mbtk_coap_ecoaprxget_exec_cmd
1118(
1119 int len
1120)
1121{
1122 int result = 0;
b.liu9e8584b2024-11-06 19:21:28 +08001123// int get_len = len;
b.liubcf86c92024-08-19 19:48:28 +08001124
liubin281ac462023-07-19 14:22:54 +08001125 return result;
1126
1127}
1128
1129int mbtk_coap_ecoapver_exec_cmd
1130(
1131 int version
1132)
1133{
1134 int result = 0;
1135 mbth_ecoap_package_ver.version = version;
1136 return result;
1137
1138}
1139
1140int mbtk_coap_ecoaptype_exec_cmd
1141(
1142 mbtk_coap_type type
1143)
1144{
1145 int result = 0;
1146 mbth_ecoap_package_ver.type = type;
b.liubcf86c92024-08-19 19:48:28 +08001147
liubin281ac462023-07-19 14:22:54 +08001148 return result;
1149
1150}
1151
1152
1153int mbtk_coap_ecoapcode_exec_cmd
1154(
1155 mbtk_coap_code_type code
1156)
1157{
1158 int result = 0;
1159 mbth_ecoap_package_ver.code = code;
1160 LOGE("ver:%d,type:%d,code:%d,msg:%d\n",
1161 mbth_ecoap_package_ver.version,
1162 mbth_ecoap_package_ver.type,
1163 mbth_ecoap_package_ver.code,
1164 mbth_ecoap_package_ver.message_id);
1165 return result;
1166
1167}
1168
1169int mbtk_coap_ecoaptoken_exec_cmd
1170(
1171 char *token_buf, int len
1172)
1173{
1174 int result = 0;
1175 uint16 token_length = len;
b.liubcf86c92024-08-19 19:48:28 +08001176 char * token = token_buf;
liubin281ac462023-07-19 14:22:54 +08001177 if(strlen(token) != token_length)
1178 {
1179 return -1;
1180 }
b.liubcf86c92024-08-19 19:48:28 +08001181
liubin281ac462023-07-19 14:22:54 +08001182 mbth_ecoap_package_ver.token_len = token_length;
1183 memcpy(mbth_ecoap_package_ver.token, token, strlen(token));
1184
1185 return result;
1186}
1187
1188int mbtk_coap_ecoapmsgid_exec_cmd
1189(
1190 int msg_id
1191)
1192{
1193 int result = 0;
b.liubcf86c92024-08-19 19:48:28 +08001194 mbth_ecoap_package_ver.message_id = msg_id;
liubin281ac462023-07-19 14:22:54 +08001195
1196 return result;
1197}
1198
1199int mbtk_coap_ecoapopt_exec_cmd
1200(
1201 char *value_buf, int buf_len
1202)
1203{
1204 int result = 0;
1205 int opt = buf_len;
1206 char* value = value_buf;
1207
1208 if(value == NULL)
1209 {
1210 return -1;
1211 }
1212 if(opt != strlen(value))
1213 {
1214 return -1;
1215 }
1216
1217 memset(&mbtk_ecoap_option_2, 0x0, sizeof(mbtk_ecoap_opt_2));
1218 memset(&mbtk_ecoap_option_3, 0x0, sizeof(mbtk_ecoap_opt_3));
1219
1220 mbtk_ecoap_option_3.optlen = opt;
1221 mbtk_ecoap_option_2.optlen = opt / 2;
1222
1223 if(mbtk_ecoap_option_2.optlen > 0)
1224 {
1225 memcpy(mbtk_ecoap_option_3.optVal, value, opt);
1226 mbtk_ecoap_option_3.optVal[opt] = '\0';
1227 StrToHex((byte*)mbtk_ecoap_option_2.optVal, (byte*)value, strlen(value));
1228// printf("\r\nopt2---------option_len,option_data");
1229// coap_log(FALSE,(char*)mbtk_ecoap_option_2.optVal,mbtk_ecoap_option_2.optlen);
1230 }
1231 return result;
1232
1233}
1234
1235int mbtk_coap_ecoapsend_exec_cmd
1236(
1237 int message_id, int Data_len, char *data
1238)
1239{
1240 int data_len = Data_len;
1241 char * send_data = NULL;
1242 int err = -1;
1243 if(data_len != 0 )
1244 {
1245 send_data = data;
1246 }
1247 int coap_id = message_id;
1248
1249 LOGE("send---------coap_id:%d,data_len:%d\n",coap_id,data_len);
1250
1251 if(mbtk_ecoap_state <= MBTK_ECOAP_STATE_CLOSING )
1252 {
1253 mbtk_ecoap_show_error(MBTK_ECOAP_ERR_NO_OPEN);
1254 return -1;
1255 }
1256
1257 if(coap_id != mbth_ecoap_package_ver.message_id)
1258 {
1259 mbtk_ecoap_show_error(MBTK_ECOAP_ERR_MSG_ID);
1260 return -1;
1261 }
1262
1263 if(coap_id != mbtk_ecoap_net.message_id)
1264 {
1265 printf("send_cmd del message_id:%d\n",mbtk_ecoap_net.message_id);
1266 mbtk_ecoap_list_del(mbtk_ecoap_net.message_id,-1);
1267 }
1268
1269 if(send_data)
1270 {
1271 if(data_len != strlen(send_data) || data_len > 1024)
1272 {
1273 mbtk_ecoap_show_error(MBTK_ECOAP_ERR_PAYLOAD_LENGTH);
1274 return -1;
1275 }
1276 }
1277
1278 mbtk_ecoap_package_s *coap = mbtk_ecoap_get_by_msg_id(coap_id);
1279 if( !coap)
1280 {
1281 mbtk_ecoap_net.message_id = mbth_ecoap_package_ver.message_id;
1282 coap = mbtk_ecoap_list_add(mbtk_ecoap_net.message_id);
1283 if(coap != NULL)
1284 {
1285 mbtk_ecoap_list_item_init(ECOAPVER,coap,mbth_ecoap_package_ver.version);
1286 mbtk_ecoap_list_item_init(EOCAPTYPE,coap,mbth_ecoap_package_ver.type);
1287 mbtk_ecoap_list_item_init(ECOAPCODE,coap,mbth_ecoap_package_ver.code);
1288 if(mbth_ecoap_package_ver.token_len > 0)
1289 {
1290 coap->token_len = mbth_ecoap_package_ver.token_len;
1291 memcpy(coap->token, mbth_ecoap_package_ver.token, strlen((char *)mbth_ecoap_package_ver.token));
1292 }
b.liubcf86c92024-08-19 19:48:28 +08001293
liubin281ac462023-07-19 14:22:54 +08001294 if(data_len > 0)
1295 {
1296 coap->payload_len = data_len;
1297 if(coap->payload != NULL)
1298 {
1299 free(coap->payload);
1300 coap->payload = NULL;
1301 }
1302 coap->payload = (uint8*)malloc(data_len + 1);
1303 memset(coap->payload,0x00,data_len + 1);
1304 memcpy(coap->payload,send_data,data_len);
1305 }
1306
1307 }
1308 else{
1309 printf("coap new error\n");
1310 }
1311
1312 }
1313 else
1314 {
1315 printf("coap is NULL\n");
1316 }
1317
1318 mbtk_ecoap_recv = 0;
1319 if(mbtk_ecoap_state < MBTK_ECOAP_STATE_READY)
1320 {
1321 printf("State[%d] error,not ready...\n",mbtk_ecoap_state);
1322 return -1;
1323 }
1324
1325 if( !ecoap_sig_send_complete_handle() )
1326 {
1327 ecoap_sig_recv_handle();
1328 }
1329
1330 if(mbtk_sock_close(coap_handle, coap_fd,1000, &err)) {
1331 return -1;
1332 }
1333
1334 coap_fd = -1;
1335 return 0;
1336
1337}
1338
1339int mbtk_coap_ecoapdel_exec_cmd( int del_id )
1340{
1341
1342 int result = 0;
1343 LOGE("del---------del_id:%d\n",del_id);
1344 if(del_id != 1)
1345 {
1346 return -1;
1347 }
b.liubcf86c92024-08-19 19:48:28 +08001348
liubin281ac462023-07-19 14:22:54 +08001349 if(mbtk_ecoap_state <= MBTK_ECOAP_STATE_CLOSING )
1350 {
1351 mbtk_ecoap_show_error(MBTK_ECOAP_ERR_NO_OPEN);
1352 return -1;
1353 }
b.liubcf86c92024-08-19 19:48:28 +08001354
liubin281ac462023-07-19 14:22:54 +08001355 LOGE("del---------del_id:%d\n",del_id);
1356
1357 return result;
1358
1359}
1360
1361int mbtk_coap_ecoapclose_exec_cmd( void )
1362{
1363 int result = 0;
1364 if(!coap_sock_inited) {
1365 LOGE("HTTP not inited.");
1366 return -1;
1367 }
1368
1369 int err = mbtk_sock_deinit(coap_handle);
1370 if(err != MBTK_SOCK_SUCCESS) {
1371 LOGE("mbtk_sock_deinit() fail.");
1372 return -1;
1373 }
1374
1375 coap_handle = -1;
1376 coap_sock_inited = FALSE;
1377 return 0;
1378
1379 return result;
1380}
1381
b.liubcf86c92024-08-19 19:48:28 +08001382void mbtk_coap_lib_info_print()
1383{
1384 MBTK_SOURCE_INFO_PRINT("mbtk_coap_lib");
1385}
liubin281ac462023-07-19 14:22:54 +08001386