-
Notifications
You must be signed in to change notification settings - Fork 0
/
coap_options.c
116 lines (90 loc) · 2.33 KB
/
coap_options.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
/*
* coap_options.c
*
* Created on: Aug 4, 2013
* Author: poppe
*/
#include "coap.h"
//TODO: make sure it return alll the options with that resource num
coap_option_t *coap_findOption(coap_pkt_t *pkt, uint16_t num)
{
RT_ASSERT(pkt);
coap_option_t *opt = pkt->options;
for(;opt; opt = opt->next)
{
if(opt->num == num)
return opt;
}
return NULL;
}
void coap_addOption(coap_pkt_t *pkt, uint8_t optNum, uint8_t *data, uint32_t len, uint8_t optFormat)
{
RT_ASSERT(pkt);
coap_option_t *opt = (coap_option_t *)coap_malloc(sizeof(coap_option_t) + len);
coap_option_t *tempOpt;
opt->num = optNum;
opt->len = len;
opt->format = optFormat;
opt->next = NULL;
memcpy(opt->value, data, len);
//Need to make sure that the options are in ascending order also if the number is the same put it after existing
tempOpt = pkt->options;
if(tempOpt == NULL)
pkt->options = opt;
else
{
while(tempOpt->num <= opt->num )
SLL_NEXT(tempOpt);
sll_insertBefore(&pkt->options, opt, tempOpt);
}
}
void coap_addOptionList(coap_option_t **optList, uint8_t optNum, uint8_t *data, uint32_t len, uint8_t optFormat)
{
RT_ASSERT(optList);
coap_option_t *opt = (coap_option_t *)coap_malloc(sizeof(coap_option_t) + len);
coap_option_t *tempOpt = *optList;
opt->num = optNum;
opt->len = len;
opt->format = optFormat;
opt->next = NULL;
memcpy(opt->value, data, len);
//Need to make sure that the options are in ascending order also if the number is the same put it after existing
if(tempOpt == NULL)
*optList = opt;
else
{
while(tempOpt->num <= opt->num )
SLL_NEXT(tempOpt);
sll_insertBefore(optList, tempOpt, opt);
}
}
void coap_addOptionToList(coap_option_t **optList, coap_option_t *opt)
{
coap_option_t *tempOpt = *optList;
if(tempOpt == NULL)
*optList = opt;
else
{
while(tempOpt->num <= opt->num )
{
if(tempOpt->next == NULL);
{
tempOpt->next = opt;
return;
}
SLL_NEXT(tempOpt);
}
sll_insertBefore(optList, tempOpt, opt);
}
}
coap_option_t *coap_createOption(uint8_t optNum, uint8_t *data, uint32_t len, uint8_t optFormat)
{
coap_option_t *opt = (coap_option_t *)coap_malloc(sizeof(coap_option_t) + len);
rt_memset(opt, '\0', (sizeof(coap_option_t) + len));
opt->num = optNum;
opt->len = len;
opt->format = optFormat;
opt->next = NULL;
memcpy(opt->value, data, len);
return opt;
}