aboutsummaryrefslogtreecommitdiffstats
path: root/usr/space_light/src/device.c
blob: 8b87879697cd2a1c52ed5207ed6103e6df10ce13 (plain) (blame)
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/*
 * The BLE code and the stripped down version of the SDK was taken and modified
 * from here
 * https://www.elektroda.com/rtvforum/topic3989434.html#20742145
 *
 * Copyright (C) 2023 jitsirakowsk (Alex)
 *
 * The original code was taken modified and used for this project by
 * Copyright (C) 2025 Bernhard Guillon <Bernhard.Guillon@begu.org>
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, version 3.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <https://www.gnu.org/licenses/>.
 *
 * SPDX: GPL-3.0
 *
 */

#include "ble.h"
#include "ble_api.h"
#include "ble_pub.h"
#include "comm.h"
#include "comm_task.h"
#include "common_bt.h"
#include "gattc.h"
#include "gattc_task.h"
#include "prf.h"

#include "application.h"
#include "generic.h"
#include <string.h>

#include "app.h"
#include "driver_pub.h"
#include "func_pub.h"
#include "include.h"
#include "mcu_ps_pub.h"
#include "start_type_pub.h"

#include "space_light.h"

#define ATT_DB_LENGTH 5
#define BK_ATT_DECL_PRIMARY_SERVICE_128                                        \
  {0x00, 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
#define BK_ATT_DECL_CHARACTERISTIC_128                                         \
  {0x03, 0x28, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
#define BK_ATT_DESC_CLIENT_CHAR_CFG_128                                        \
  {0x02, 0x29, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
#define WRITE_REQ_CHARACTERISTIC                                               \
  {0xD0, 0x07, 0x9B, 0x5F, 0x80, 0x00, 0x01, 0x80,                             \
   0x01, 0x10, 0,    0,    0x01, 0,    0,    0}

typedef struct ble_cfg_st {
  struct bd_addr mac;
  char name[APP_DEVICE_NAME_LENGTH_MAX];
} BLE_CFG_ST, *BLE_CFG_PTR;

uint32_t app_stack_size = 4096;
uint32_t ext_init_stack_size = 2048;
char canary[2] = {'N', '\0'};

extern uint32_t _bootloader_world_flag;

volatile uint32_t *bootloader_world_flag = &_bootloader_world_flag;
extern BLE_CFG_ST ble_cfg;
extern struct bd_addr common_default_bdaddr;

extern struct prf_env_tag prf_env;

extern uint8_t bk_ble_get_prf_by_task(kernel_task_id_t task,
                                      struct prf_task_env **env);

static void *pull_stubs(void);

bk_attm_desc_t btl_att_db[ATT_DB_LENGTH] = {
    [0] = {BK_ATT_DECL_PRIMARY_SERVICE_128, BK_PERM_SET(RD, ENABLE), 0, 0},
    [1] = {BK_ATT_DECL_CHARACTERISTIC_128, BK_PERM_SET(RD, ENABLE), 0, 0},
    [2] = {WRITE_REQ_CHARACTERISTIC,
           BK_PERM_SET(WRITE_REQ, ENABLE) | BK_PERM_SET(WRITE_COMMAND, ENABLE),
           BK_PERM_SET(RI, ENABLE) | BK_PERM_SET(UUID_LEN, UUID_128), 256},
    [3] = {BK_ATT_DECL_CHARACTERISTIC_128, BK_PERM_SET(RD, ENABLE), 0, 0},
    [4] = {BK_ATT_DESC_CLIENT_CHAR_CFG_128,
           BK_PERM_SET(RD, ENABLE) | BK_PERM_SET(WRITE_REQ, ENABLE), 0, 0},
};

beken_semaphore_t app_sema = NULL;
beken_semaphore_t ext_init_sema = NULL;

static void ble_event_callback(ble_event_t event, void *param) {
  switch (event) {
  case BLE_STACK_OK:
    bk_printf("STACK INIT OK\r\n");

    {
      struct bk_ble_db_cfg ble_db_cfg;

      ble_db_cfg.att_db = btl_att_db;
      ble_db_cfg.att_db_nb = ATT_DB_LENGTH;
      ble_db_cfg.prf_task_id = 0;
      ble_db_cfg.start_hdl = 0;
      ble_db_cfg.svc_perm = BK_PERM_SET(SVC_UUID_LEN, UUID_16);
      bk_ble_create_db(&ble_db_cfg);
    }

    break;

  case BLE_CREATE_DB_OK:
    bk_printf("CREATE DB SUCCESS\r\n");
    appm_start_advertising();
    break;

  case BLE_CONNECT:
    bk_printf("BLE CONNECT\r\n");
    if (((struct gapc_connection_req_ind *)param)->sup_to < 200) {
      struct gapc_conn_param conn_param;

      conn_param.intv_max =
          ((struct gapc_connection_req_ind *)param)->con_interval;
      conn_param.intv_min =
          ((struct gapc_connection_req_ind *)param)->con_interval;
      conn_param.latency =
          ((struct gapc_connection_req_ind *)param)->con_latency;
      conn_param.time_out = 600;
      appm_update_param(&conn_param);
    }

    mcu_prevent_set(MCU_PS_BLE_FROBID);
    break;

  case BLE_MTU_CHANGE:
    bk_printf("BLE_MTU_CHANGE:%d\r\n", *(unsigned short *)param);
    break;

  case BLE_DISCONNECT:
    bk_printf("BLE DISCONNECTed\r\n");
    break;

  case BLE_CFG_NOTIFY:
    bk_printf("BLE_CFG_NOTIFY:%d\r\n", *(unsigned short *)param);
    break;

  case BLE_CFG_INDICATE:
    bk_printf("BLE_CFG_INDICATE:%d\r\n", *(unsigned short *)param);
    break;

  case BLE_TX_DONE:
    bk_printf("BLE_TX_DONE\r\n");
    break;

  case BLE_GEN_DH_KEY:
    bk_printf("BLE_GEN_DH_KEY\r\n");
    break;

  case BLE_GET_KEY:
    bk_printf("BLE_GET_KEY\r\n");
    break;

  case BLE_STACK_FAIL:
    bk_printf("STACK INIT FAIL\r\n");
    break;

  case BLE_HW_ERROR:
    bk_printf("BLE_HW_ERROR\r\n");
    break;

  default:
    bk_printf("UNKNOWN EVENT:%d\r\n", event);
    break;
  }
}

#define CMD_RGBCW 0x1

static void ble_write_callback(write_req_t *write_req) {
  switch (write_req->value[0]) {
  case 0x1: {
    if (write_req->len != 6) {
      return;
    }
    struct RGBCW rgbcw = {
        write_req->value[1], write_req->value[2], write_req->value[3],
        write_req->value[4], write_req->value[5],
    };
    set_rgbcw(&rgbcw);
    break;
  }
  case 0x2:
    if (write_req->len != 3) {
      return;
    }
    uint16_t seconds = write_req->value[1] << 8 | write_req->value[2];
    set_timer(seconds);
    break;
  case 0xFF:
    setPWM(&write_req->value[1], write_req->len - 1);
    break;
  default:
    break;
  }
}

static unsigned char ble_read_callback(read_req_t *read_req) {
  // TODO: implement error codes for commands
  return 2;
}

static void ble_recv_adv_callback(recv_adv_t *recv_adv) {}

static void init_ble_thread(void *arg) {

  ble_set_event_cb(ble_event_callback);

  ble_set_write_cb(ble_write_callback);
  ble_set_read_cb(ble_read_callback);
  ble_set_recv_adv_cb(ble_recv_adv_callback);

  ble_clk_power_up();
  ble_switch_rf_to_ble();
  ble_init();
  ble_activate("SPACELIGHT");

  uint8_t *mac = (uint8_t *)&ble_cfg.mac;

  bk_printf("ble name:%s, %02x:%02x:%02x:%02x:%02x:%02x\r\n", &ble_cfg.name,
            mac[5], mac[4], mac[3], mac[2], mac[1], mac[0]);

  rtos_delete_thread(NULL);
}

static void ext_init_task_handler(void *arg) {
  OSStatus ret;

  func_init_extended();
  ret = rtos_set_semaphore(&ext_init_sema);

  ASSERT(kNoErr == ret);

  rtos_delete_thread(NULL);
}

void app_set_sema(void) {
  OSStatus ret;

  ret = rtos_set_semaphore(&app_sema);

  (void)ret;
}

__attribute__((noinline)) static void *pull_stubs(void) {
  int a = 0;
  extern uint32_t _vector_start;
  uint32_t *vecs = &_vector_start;
  extern uint8_t wifi_read_efuse(uint8_t addr);
  uint8_t ef = 0;

  ef = wifi_read_efuse(31);
  bk_printf("start: %x, ef: %x, a: %d\n", *vecs, ef, a);
  return NULL;
}

void entry_main(void) {

  OSStatus ret;

  canary[0] = 'm';
  *bootloader_world_flag = 1;

  bk_misc_init_start_type();
  driver_init();
  bk_misc_check_start_type();
  func_init_basic();

  pull_stubs();

  uint32_t my_stack = 0;
  __asm__ __volatile__("mov %0, sp" : "=r"(my_stack)::);

  bk_printf("current stack top: 0x%x\n", my_stack);

  int a = 0x7FFFFFFF;
  int b[3] = {1, 2, 3};

  if (a + 2 [b] >= 0) {
    bk_printf("condition (%d >= 0) is true\n", a + 2 [b]);
  }

  union u_t {
    uint32_t a;
    struct s_t {
      uint16_t b;
      uint16_t c;
    } d;
  } u;

  u.d = (struct s_t){0, 1};

  bk_printf("value: 0x%x, system is %s endian\n", u.a,
            u.a == 1 ? "big" : "little");

#define IS_ARM()                                                               \
  do {                                                                         \
    uint32_t is_arm = 0;                                                       \
    __asm__ __volatile__("mov %0, pc\n"                                        \
                         "mov r1, pc\n"                                        \
                         "sub %0, r1, %0\n"                                    \
                         "sub %0, #2\n"                                        \
                         : "=r"(is_arm)::"r1");                                \
    bk_printf("current state is %s\n", is_arm ? "ARM" : "Thumb");              \
  } while (0)

  extern uint32_t is_arm(void);

  bk_printf("is_arm() runs in %s state\n", is_arm() ? "ARM" : "Thumb");
  IS_ARM();

  ret = rtos_create_thread(NULL, THD_INIT_PRIORITY, "main",
                           (beken_thread_function_t)init_ble_thread,
                           app_stack_size, (beken_thread_arg_t)0);
  ASSERT(kNoErr == ret);

  rtos_init_semaphore(&ext_init_sema, 1);
  ret = rtos_create_thread(NULL, THD_EXTENDED_APP_PRIORITY, "ext_init",
                           (beken_thread_function_t)ext_init_task_handler,
                           ext_init_stack_size, (beken_thread_arg_t)0);
  ASSERT(kNoErr == ret);

  ret = rtos_create_thread(NULL, THD_INIT_PRIORITY, "main",
                           (beken_thread_function_t)init_led_thread,
                           app_stack_size, (beken_thread_arg_t)0);
  ASSERT(kNoErr == ret);

  vTaskStartScheduler();
}