1 /*
2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited
3 */
4
5
6
7
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <errno.h>
12
13 #include "iotx_coap_internal.h"
14 #include "CoAPExport.h"
15 #include "CoAPPlatform.h"
16 #include "CoAPNetwork.h"
17
CoAPNetwork_read(NetworkContext * p_context,NetworkAddr * p_remote,unsigned char * p_data,unsigned int datalen,unsigned int timeout_ms)18 int CoAPNetwork_read(NetworkContext *p_context, NetworkAddr *p_remote,
19 unsigned char *p_data, unsigned int datalen,
20 unsigned int timeout_ms)
21
22 {
23 int len = 0;
24 NetworkConf *network = NULL;
25
26 if (NULL == p_context || NULL == p_remote || NULL == p_data) {
27 return -1; /* TODO */
28 }
29
30 network = (NetworkConf *)p_context;
31 #ifdef COAP_DTLS_SUPPORT
32 if (COAP_NETWORK_DTLS == network->type) {
33 len = 0;
34 } else {
35 #endif
36 len = HAL_UDP_recvfrom(network->fd, p_remote, p_data, datalen,
37 timeout_ms);
38 /* COAP_DEBUG("[CoAP-NWK]: Network read return %d", len); */
39 #ifdef COAP_DTLS_SUPPORT
40 }
41 #endif
42 return len;
43 }
44
CoAPNetwork_write(NetworkContext * p_context,NetworkAddr * p_remote,const unsigned char * p_data,unsigned int datalen,unsigned int timeout_ms)45 int CoAPNetwork_write(NetworkContext *p_context, NetworkAddr *p_remote,
46 const unsigned char *p_data, unsigned int datalen,
47 unsigned int timeout_ms)
48
49 {
50 int len = 0;
51 NetworkConf *network = NULL;
52
53 if (NULL == p_context || NULL == p_remote || NULL == p_data) {
54 return -1; /* TODO */
55 }
56
57 network = (NetworkConf *)p_context;
58 #ifdef COAP_DTLS_SUPPORT
59 /* TODO: */
60 if (COAP_NETWORK_DTLS == network->type) {
61 len = 0;
62 } else {
63 #endif
64 len =
65 HAL_UDP_sendto(network->fd, p_remote, p_data, datalen, timeout_ms);
66 #ifdef COAP_DTLS_SUPPORT
67 }
68 #endif
69 return len;
70 }
71
CoAPNetwork_init(const NetworkInit * p_param)72 NetworkContext *CoAPNetwork_init(const NetworkInit *p_param)
73 {
74 NetworkConf *network = NULL;
75
76 if (NULL == p_param) {
77 return NULL;
78 }
79
80 network = coap_malloc(sizeof(NetworkConf));
81 if (NULL == network) {
82 return NULL;
83 }
84
85 memset(network, 0x00, sizeof(NetworkConf));
86 network->type = p_param->type;
87
88 #ifdef COAP_DTLS_SUPPORT
89 if (COAP_NETWORK_DTLS == network->type) {
90 /* TODO: */
91 coap_free(network);
92 return NULL;
93 } else {
94 #endif
95 /*Create udp socket*/
96 network->port = p_param->port;
97 network->fd =
98 (intptr_t)HAL_UDP_create_without_connect(NULL, network->port);
99 if ((intptr_t)-1 == network->fd) {
100 coap_free(network);
101 return NULL;
102 }
103
104 HAL_UDP_joinmulticast(network->fd, p_param->group);
105 #ifdef COAP_DTLS_SUPPORT
106 }
107 #endif
108 return (NetworkContext *)network;
109 }
110
CoAPNetwork_deinit(NetworkContext * p_context)111 void CoAPNetwork_deinit(NetworkContext *p_context)
112 {
113 NetworkConf *network = NULL;
114 if (NULL == p_context) {
115 return;
116 }
117
118 network = (NetworkConf *)p_context;
119 #ifdef COAP_DTLS_SUPPORT
120 if (COAP_NETWORK_DTLS == network->type) {
121 /* TODO: */
122 } else {
123 #endif
124 HAL_UDP_close_without_connect(network->fd);
125 coap_free(p_context);
126 p_context = NULL;
127 #ifdef COAP_DTLS_SUPPORT
128 }
129 #endif
130 return;
131 }
132