Browse Source

✨ feat(): Add chipset

添加了 Chipset 层,并且实现了 Zephyr Controller 的 chipset 移植
Jackistang 4 years ago
parent
commit
079b9f3569

+ 3 - 0
.gitmodules

@@ -0,0 +1,3 @@
+[submodule "3rd-party/mpool"]
+	path = 3rd-party/mpool
+	url = https://github.com/isayme/mpool.git

+ 1 - 0
3rd-party/mpool

@@ -0,0 +1 @@
+Subproject commit 195047c56221776ccf241aef1f2de6286b5f54ec

+ 0 - 0
3rd-party/ringbuffer.c → 3rd-party/ringbuffer/ringbuffer.c


+ 0 - 0
3rd-party/ringbuffer.h → 3rd-party/ringbuffer/ringbuffer.h


+ 8 - 2
CMakeLists.txt

@@ -6,6 +6,7 @@ set(CMAKE_BUILD_TYPE "Debug")
 
 set(SOURCES
     # src/hci_transport_uart_linux.c
+    src/chipset.c
     src/hci_transport_h4.c
     src/receiver.c
 
@@ -16,7 +17,11 @@ set(SOURCES
     porting/os/linux/src/os_uart.c
 
 # 3rd-party
-    3rd-party/ringbuffer.c
+    3rd-party/ringbuffer/ringbuffer.c
+    3rd-party/mpool/mpool.c
+
+# chipset
+    chipset/zephyr/chipset_zephyr.c
 
 # test
     tests/test.c
@@ -34,7 +39,8 @@ target_include_directories(hci_middleware
     ${PROJECT_SOURCE_DIR}/porting/os/linux/include
 
 # 3rd-party
-    ${PROJECT_SOURCE_DIR}/3rd-party/
+    ${PROJECT_SOURCE_DIR}/3rd-party/ringbuffer
+    ${PROJECT_SOURCE_DIR}/3rd-party/mpool
 )
 
 target_link_libraries(hci_middleware

+ 54 - 0
chipset/zephyr/chipset_zephyr.c

@@ -0,0 +1,54 @@
+#include "chipset.h"
+#include <assert.h>
+#include <string.h>
+
+#define ARRAY_SIZE(array)   ((sizeof(array) / sizeof(array[0])))
+
+static const uint8_t g_init_command[] = {
+    // OGF: 0x3F, OCF: 0x09, Paramter Total Length: 0x00. 
+    // Zephyr Controller 
+    0x09, 0xfc, 0x00, 
+};
+
+static uint16_t g_index;
+
+static void init(void *args)
+{
+    g_index = 0;
+    return ;
+}
+
+static int next_hci_command(uint8_t *buf)
+{
+    assert(buf);
+
+    if (g_index >= ARRAY_SIZE(g_init_command))
+        return CHIPSET_ITER_STOP;
+    
+    // Copy command header.
+    memcpy(&buf[0], &g_init_command[g_index], 3);
+    g_index += 3;
+
+    // Copy command payload.
+    uint16_t payload_len = buf[2];
+    memcpy(&buf[3], &g_init_command[g_index], payload_len);
+    g_index += payload_len;
+ 
+    return CHIPSET_ITER_CONTINUE;
+}
+
+
+static struct rt_chipset chipset_zephyr = {
+    .name             = "Zephyr",
+    .init             = init,
+    .next_hci_command = next_hci_command,
+};
+
+rt_chipset_t* rt_chipset_get_instance(void)
+{
+    return &chipset_zephyr;
+}
+
+
+
+

+ 13 - 11
include/chipset.h

@@ -12,7 +12,7 @@ enum {
     CHIPSET_ITER_CONTINUE,
 };
 
-struct rt_chipset {
+typedef struct rt_chipset {
     char *name;
 
     /**
@@ -20,7 +20,7 @@ struct rt_chipset {
      * 
      * @return void
     */
-    void (*init)(void);
+    void (*init)(void *args);
 
     /**
      * @brief Send vendor HCI command.
@@ -32,25 +32,27 @@ struct rt_chipset {
      * @retval  CHIPSET_ITER_CONTINUE   Continue send command.
      * 
     */
-    int (*next_hci_command)(const uint8_t **buf);
+    int (*next_hci_command)(uint8_t *buf);
 
-};
+} rt_chipset_t;
 
 /**
- * @brief Register a chipset handle.
- * 
- * @param[in] chipset   A chipset handle, should be static memory.
+ * @brief Get a chipset instance.
  * 
- * @return void
+ * @return rt_chipset_t *
+ * @retval  Non-NULL on success.
+ * @retval  NULL on fail.
 */
-extern void rt_chipset_register(struct rt_chipset *chipset);
+extern rt_chipset_t* rt_chipset_get_instance(void);
 
 /**
  * @brief Init chipset.
  * 
- * @return void
+ * @return int
+ * @retval  0   Success
+ * @retval  -1  Fail
 */
-extern void rt_chipset_init(void);
+extern int rt_chipset_init_start(void);
 
 
 #ifdef __cplusplus

+ 46 - 0
src/chipset.c

@@ -0,0 +1,46 @@
+#include "chipset.h"
+#include "os_port.h"
+#include "hci_transport_h4.h"
+#include <assert.h>
+
+static uint8_t g_hci_command_buf[260];
+static struct os_sem sync_sem;
+
+
+static void chipset_recv_handler(int packet_type, uint8_t *packet, uint16_t size)
+{
+    assert(packet_type == HCI_TRANSPORT_H4_EVENT); //  Event
+
+    if (packet[0] == 0x0e)  //  Command complete
+        os_sem_release(&sync_sem);
+}
+
+int rt_chipset_init_start(void)
+{
+    int ret = CHIPSET_ITER_CONTINUE;
+    uint16_t len = 0;   // HCI command length.
+    int err = 0;
+
+    os_sem_init(&sync_sem, 0);
+
+    rt_hci_transport_h4_register_packet_handler(chipset_recv_handler);
+
+    rt_chipset_t *instance = rt_chipset_get_instance();
+    assert(instance);
+
+    instance->init(NULL);
+    
+    while (ret == CHIPSET_ITER_CONTINUE) {
+        ret = instance->next_hci_command(g_hci_command_buf);    // 1 byte is for H4 type.   
+        len = 3 + g_hci_command_buf[2];     // Opcode(2 bytes) + Parameter length(1 byte) + Payload length
+
+        err = rt_hci_transport_h4_send(HCI_TRANSPORT_H4_COMMAND, g_hci_command_buf, len);
+        if (err)
+            return -1;
+
+        err = os_sem_take(&sync_sem, 1000);
+        if (err)
+            return -1;
+
+    }
+}