Blame view

orbimote/field_test_device/benchmark.c 6.77 KB
ca223e024   Jean-Michel Friedt   orbimote avec sof...
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  
  
  
  #define ENABLE_DEBUG (1)
  #include "debug.h"
  
  #include "benchmark.h"
  
  #include "xtimer.h"
  #include <time.h>
  
  #include <string.h>
  
  #include "net/loramac.h"
  #include "semtech_loramac.h"
  #include "loramac_utils.h"
  #include "app_clock.h"
  
  #include <random.h>
  
  // Count the number of elements in an array.
  //#define CNT(array) (uint8_t)(sizeof(array) / sizeof(*array))
  
  #define PAYLOAD_LEN	256
  
  static uint8_t payload[PAYLOAD_LEN];
  
  // Encode message data to the payload.
  unsigned int encode_benchmark(uint8_t *payload, unsigned int len, uint8_t power, uint8_t dr)
  {
  
  	// reset the payload
  	memset(payload,0,PAYLOAD_LEN);
  
  	if(len < sizeof(2 * sizeof(uint8_t))) {
  		return 0;
  	}
  
  	unsigned int i = 0;
  
  	// Encode txpower.
  	payload[i++] = power;
      // Encode datarate.
  	payload[i++] = dr;
  
  	// TODO add FCNT, RSSI, LSNR of the last downlink.
  
  	return i;
  }
  
  void benchmark_start(semtech_loramac_t *loramac, struct benchmark_t benchmark, unsigned int (*encode_sensors)(uint8_t*, const unsigned int)) {
  
      // Start benchmark
      DEBUG("[ftd] Start benchmark
  ");
  
  
      /* set ADR flag */
      semtech_loramac_set_adr(loramac, benchmark.adr);
  
      uint8_t port = benchmark.min_port;
      uint32_t cpt = 0;
      while (1)
      {
          port = benchmark.min_port + ((port + 1 - benchmark.min_port) % (benchmark.max_port - benchmark.min_port));
  
          int i;
          uint8_t dr;
          uint8_t power;
          uint8_t size;
  
          DEBUG("[ftd] New benchmark sequence: port=%d
  ", port);
  
          for( i=0 ; i < benchmark.drpwsz_sequence_nb  ; i++) {
  
          	cpt++;
  
              dr = benchmark.drpwsz_sequence[3*i];
              power = benchmark.drpwsz_sequence[3*i+1];
              size = benchmark.drpwsz_sequence[3*i+2];
  
              // TODO uint32_t devaddr = devaddrs[cpt%ARRAYSIZE(devaddrs)];
              uint32_t devaddr = benchmark.devaddr + (cpt%benchmark.nb_virtual_devices);
  
          	DEBUG("[ftd] Send @ devaddr=%lx port=%d dr=%d txpower=%d size=%d
  ", devaddr, port, dr, power, size);
  
          	unsigned int len = encode_benchmark(payload, size, power, dr);
  
          	len = encode_sensors(payload + len, size - len);
  
              // WARNING : If LORAMAC_TX_CNF, the firmware is blocked when the network server does not confirmed the message
              //semtech_loramac_set_tx_mode(loramac, benchmark.txconfirmed ? LORAMAC_TX_CNF : LORAMAC_TX_UNCNF);
  
              /* send the LoRaWAN message */
          	if(dr == 0xff) {
          	    semtech_loramac_set_adr(loramac, true);
          	} else {
          	    semtech_loramac_set_adr(loramac, false);
          		semtech_loramac_set_dr(loramac, dr);
          	}
  
              semtech_loramac_set_tx_port(loramac, port);
              semtech_loramac_set_tx_power(loramac, power);
  
              semtech_loramac_set_devaddr(loramac, (uint8_t*)&devaddr);
  
              uint8_t ret = semtech_loramac_send(loramac, payload, size);
  
  
              if (ret != SEMTECH_LORAMAC_TX_DONE) {
                  DEBUG("[ftd] ERROR: Cannot send payload: ret code: %d (%s)
  ", ret, loramac_utils_err_message(ret));
              } else {
              	DEBUG("[ftd] Tx Done ret=%d
  ", ret);
              }
  
              xtimer_sleep(*benchmark.tx_period);
  
              // send a APP_TIME_REQ request every APP_TIME_REQ_PERIOD message
              if(cpt%APP_TIME_REQ_PERIOD == 0) {
              	// keep the current MAC configuration
                  //semtech_loramac_set_tx_mode(loramac, LORAMAC_TX_CNF);
              	app_clock_send_app_time_req(loramac);
              	xtimer_sleep(*benchmark.tx_period);
              }
  
          }
  
          /* sleep tx_period secs */
          // TODO introduire un alea de quelques secondes dans la tx_period pour éviter que des endpoints qui redémarrent ensemble se brouillent les uns les autres.
          // TODO verifier que la tx_period est compatible avec le DC (sinon, le Tx retourne le code=13)
          xtimer_usleep(*benchmark.tx_period * 1000000 + random_uint32_range (0, NEXT_BENCHMARK_RANDOM * 1000000));
  
      }
  
      /* this should never be reached */
      return;
  }
  
  
  void jmf_start(semtech_loramac_t *loramac, struct benchmark_t benchmark) {
  
      // Start benchmark
      DEBUG("[jmf] Hello\r
  ");
  
      /* set ADR flag */
      semtech_loramac_set_adr(loramac, benchmark.adr);
      uint8_t dr=5;
      uint8_t power=14;
      uint8_t size=5;
      uint8_t port = benchmark.min_port;
      char jmf_c;
      uint32_t devaddr = benchmark.devaddr ;
      port = benchmark.min_port + ((port + 1 - benchmark.min_port) % (benchmark.max_port - benchmark.min_port));
      semtech_loramac_set_adr(loramac, true);
      semtech_loramac_set_dr(loramac, dr);
  
      semtech_loramac_set_tx_port(loramac, port);
      semtech_loramac_set_tx_power(loramac, power);
  
      semtech_loramac_set_devaddr(loramac, (uint8_t*)&devaddr);
  
      while (1)
      {
  	    jmf_c =getchar();
  	    /*if ((jmf_c>32)&&(jmf_c<127))
  	        {DEBUG("%c", jmf_c);}
          else
              {DEBUG("%d", jmf_c);}*/
  
          //unsigned int len=8; //  = encode_benchmark(payload, size, power, dr);
          if (jmf_c == 'S' || jmf_c == 'T') { // 0x53 = S et 0x54 = T
  			if (jmf_c == 'S') 
  				{sprintf((char*)payload,"Start");}
  			else if (jmf_c == 'T') 
  				{sprintf((char*)payload,"Times");}
  
          // WARNING : If LORAMAC_TX_CNF, the firmware is blocked when the network server does not confirmed the message
          //semtech_loramac_set_tx_mode(loramac, benchmark.txconfirmed ? LORAMAC_TX_CNF : LORAMAC_TX_UNCNF);
              /* send the LoRaWAN message */
              uint8_t ret = semtech_loramac_send(loramac, payload, size);
         	    
  			//xtimer_sleep(3);
              //uint8_t ret = semtech_loramac_send(loramac, payload, size);
              //DEBUG("[jmf] Send @ devaddr=%lx port=%d dr=%d txpower=%d size=%d\r
  ", devaddr, port, dr, power, size);
  
              if (ret != SEMTECH_LORAMAC_TX_DONE) 
                  {DEBUG("[ftd] ERROR: Cannot send payload: ret code: %d (%s)
  ", ret, loramac_utils_err_message(ret));}
           /*   else 
              	{DEBUG("[ftd] Tx Done ret=%d\r
  ", ret);}  */
  
           } else DEBUG("\r
  ");
  	    
  /*
              xtimer_sleep(*benchmark.tx_period);
  
              // send a APP_TIME_REQ request every APP_TIME_REQ_PERIOD message
              if(cpt%APP_TIME_REQ_PERIOD == 0) {
              	// keep the current MAC configuration
                  //semtech_loramac_set_tx_mode(loramac, LORAMAC_TX_CNF);
              	app_clock_send_app_time_req(loramac);
              	xtimer_sleep(*benchmark.tx_period);
              }
  
           //sleep tx_period secs 
          // TODO introduire un alea de quelques secondes dans la tx_period pour éviter que des endpoints qui redémarrent ensemble se brouillent les uns les autres.
          // TODO verifier que la tx_period est compatible avec le DC (sinon, le Tx retourne le code=13)
          xtimer_usleep(*benchmark.tx_period * 1000000 + random_uint32_range (0, NEXT_BENCHMARK_RANDOM * 1000000));
  
      
  
       //this should never be reached */
  }
      return;
  }