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