diff --git a/Example/main.c b/Example/main.c index 4db26ef..e1ebc92 100644 --- a/Example/main.c +++ b/Example/main.c @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include "log.h" #include "libuv_dbus.h" @@ -20,6 +22,7 @@ #include "inet_api.h" #include "skins_res.h" #include "server_addr.h" +#include "boardlink_iot.h" static MOD_INFO_TABLE g_ModInfo; static uv_timer_t g_tmTest; @@ -1591,6 +1594,16 @@ static void __dlThreadRuntimeCb(void *pParams) static void __uvThreadTest(void *pParams) { + for(int i = 0; i < 100; i++) + { + char buf[256]; + memset(buf, 0, 256); + sprintf(buf, "Send %d times\n", i); + BL_SendMessage(buf, strlen(buf)); + sleep(1); + } + +#if 0 for(int i = 0; i < 100000; i++) { uv_thread_t uvThread; @@ -1608,6 +1621,7 @@ static void __uvThreadTest(void *pParams) } usleep(1000); } +#endif } static void __dlTestThread(void* p) @@ -1702,6 +1716,107 @@ static void __uvThreadTimerV2(uv_loop_t *pLoop) uv_timer_start(&uvTimer, uvTimerTestCb, 12000, 0); } +#define MAX_PAYLOAD 1024 // maximum payload size +#define NETLINK_TEST 30 //自定义的协议 + +void test_netlink(void) +{ + struct sockaddr_nl src_addr, dest_addr; + struct nlmsghdr *nlh = NULL; //Netlink数据包头 + struct msghdr msg; + int sock_fd; + int retval; + struct iovec iov; + + sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_TEST); + + if(sock_fd == -1) + { + LOG_EX(LOG_Error, "Init socket error: %s\n", strerror(errno)); + return; + } + + // To prepare binding + memset(&src_addr, 0, sizeof(src_addr)); + src_addr.nl_family = AF_NETLINK; + src_addr.nl_pid = getpid(); //A:设置源端端口号 + src_addr.nl_groups = 0; + + //Bind + retval = bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr)); + if(retval < 0) + { + LOG_EX(LOG_Error, "bind failed: %s", strerror(errno)); + close(sock_fd); + return; + } + + // To orepare create mssage + nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD)); + if(!nlh) + { + LOG_EX(LOG_Error, "malloc nlmsghdr error!\n"); + close(sock_fd); + return; + } + + memset(&dest_addr, 0, sizeof(dest_addr)); + dest_addr.nl_family = AF_NETLINK; + dest_addr.nl_pid = 0; //B:设置目的端口号 + dest_addr.nl_groups = 0; + nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD); + nlh->nlmsg_pid = getpid(); //C:设置源端口 + nlh->nlmsg_flags = 0; + strcpy(NLMSG_DATA(nlh),"Hello you!"); //设置消息体 + + iov.iov_base = (void *)nlh; + iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD); + + //Create mssage + memset(&msg, 0, sizeof(msg)); + msg.msg_name = (void *)&dest_addr; + msg.msg_namelen = sizeof(dest_addr); + msg.msg_iov = &iov; + msg.msg_iovlen = 1; + + retval = sendmsg(sock_fd, &msg, 0); + + if(retval == -1) + { + LOG_EX(LOG_Error, "sendmsg failed: %s", strerror(errno)); + } + + memset(nlh,0,NLMSG_SPACE(MAX_PAYLOAD)); + + while(TRUE) + { + retval = recvmsg(sock_fd, &msg, 0); + if(retval < 0) + { + printf("state<1"); + } + else + { + LOG_EX(LOG_Debug, "Received message: %s\n",(char *) NLMSG_DATA(nlh)); + } + } + + close(sock_fd); +} + +static void __uvThreadNetlinkSend(void *pParams) +{ + for(int i = 0; i < 100; i++) + { + char buf[256]; + memset(buf, 0, 256); + sprintf(buf, "Send %d times", i); + //LOG_EX(LOG_Debug, "Send [%s] ret = %d\n", buf, BL_SendMessage(buf, strlen(buf))); + BL_SendMessage(buf, strlen(buf)); + sleep(1); + } +} + int main(int argc, char **argv) { int i, j, ret = 0; @@ -1710,7 +1825,8 @@ int main(int argc, char **argv) uv_loop_t* pLoop = GetDBusDefaultLoop(); int modIdx = -1; char buf[256]; -#if 1 + BL_IOT_MSG blMsg; +#if 0 i = GetServerModeFromCC(PUBLISH_MODE, &ret); fprintf(stdout, "Read ServerMode = %d, errno = %d\n", i, ret); @@ -1784,9 +1900,28 @@ int main(int argc, char **argv) return 0; } + //test_netlink(); + + BL_Init(); + + memset(&blMsg, 0, sizeof(BL_IOT_MSG)); + + strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS); + + if(modIdx % 2 == 0) + { + blMsg.msgType = MSG_IN_DISCOVERY_MODE; + } + else + { + blMsg.msgType = MSG_OUT_DISCOVERY_MODE; + } + + BL_SendMessage((unsigned char*)&blMsg, sizeof(BL_IOT_MSG)); + //test_netlink(); //__uvThreadTimerV2(pLoop); - //test_task_new(__uvThreadTest, NULL); + //test_task_new(__uvThreadNetlinkSend, NULL); //DumpCurServerAddr("Default"); diff --git a/Framework/IoT/Boardlink/boardlink_iot.c b/Framework/IoT/Boardlink/boardlink_iot.c new file mode 100644 index 0000000..c28c4ad --- /dev/null +++ b/Framework/IoT/Boardlink/boardlink_iot.c @@ -0,0 +1,135 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#if defined(PLATFORM_R16) || defined (PLATFORM_CPU) +#include "log.h" +#include "libuv_dbus.h" +#include "boardlink_iot.h" +#else +#include +#include +#include +#endif + +#define BL_IOT_PROTO (30) +#define BL_MSG_BUF_MAX (1024 * 4) + +static int g_nlSock = -1; +static unsigned char g_MsgSendBuf[BL_MSG_BUF_MAX]; + +static void __recvMsgThread(void *pParam) +{ + unsigned char msgBuf[BL_MSG_BUF_MAX]; + struct iovec iov = {msgBuf, BL_MSG_BUF_MAX}; + struct nlmsghdr *pMsgHdr; + struct sockaddr_nl fromAddr; + struct msghdr msgHdr = {&fromAddr, sizeof(fromAddr), &iov, 1, NULL, 0, 0};; + + memset(&fromAddr, 0, sizeof(fromAddr)); + fromAddr.nl_family = AF_NETLINK; + fromAddr.nl_pid = 0; + fromAddr.nl_groups = 0; + + while(TRUE) + { + int len = recvmsg(g_nlSock, &msgHdr, 0); + + if(len > 0) + { + for(pMsgHdr = (struct nlmsghdr *)msgBuf; + NLMSG_OK(pMsgHdr, len); + pMsgHdr = NLMSG_NEXT(pMsgHdr, len)) + { + if(pMsgHdr->nlmsg_type == NLMSG_DONE) + { + continue; + } + else if(pMsgHdr->nlmsg_type == NLMSG_ERROR) + { + continue; + } + else + { + LOG_EX(LOG_Debug, "Recv Message: %s\n", NLMSG_DATA(pMsgHdr)); + } + } + } + + usleep(1000); + } + pthread_detach(pthread_self()); +} + +static int __nlCreateSocket(void) +{ + struct sockaddr_nl srcAddr; + + g_nlSock = socket(AF_NETLINK, SOCK_RAW, BL_IOT_PROTO); + + if(g_nlSock == -1) + { + LOG_EX(LOG_Error, "Create netlink socker error: %s\n", strerror(errno)); + return -ERR_CREATE_SOCKET; + } + + memset(&srcAddr, 0, sizeof(srcAddr)); + srcAddr.nl_family = AF_NETLINK; + srcAddr.nl_pid = getpid(); + srcAddr.nl_groups = 0; + + if(bind(g_nlSock, (struct sockaddr*)&srcAddr, sizeof(srcAddr)) < 0) + { + LOG_EX(LOG_Error, "Bind netlink socket failed: %s", strerror(errno)); + close(g_nlSock); + return -ERR_BIND_SOCKET; + } + + return 0; +} + +int BL_SendMessage(unsigned char* pData, unsigned int len) +{ + struct iovec iov; + struct sockaddr_nl dstAddr; + struct nlmsghdr* pMsgHdr = (struct nlmsghdr*)g_MsgSendBuf; + struct msghdr msgHdr = {&dstAddr, sizeof(dstAddr), &iov, 1, NULL, 0, 0}; + + memset(&dstAddr, 0, sizeof(struct sockaddr_nl)); + dstAddr.nl_family = AF_NETLINK; + dstAddr.nl_pid = 0; + dstAddr.nl_groups = 0; + + memset(pMsgHdr, 0, BL_MSG_BUF_MAX); + pMsgHdr->nlmsg_len = len; + pMsgHdr->nlmsg_pid = getpid(); + pMsgHdr->nlmsg_flags = 0; + memcpy(NLMSG_DATA(pMsgHdr), pData, len); + + iov.iov_base = pMsgHdr; + iov.iov_len = NLMSG_SPACE(BL_MSG_BUF_MAX); + + return sendmsg(g_nlSock, &msgHdr, 0); +} + +int BL_Init(void) +{ + int ret = 0; + uv_thread_t uvRecvThread; + + ret = __nlCreateSocket(); + + if(ret != 0) + { + return ret; + } + + uv_thread_create(&uvRecvThread, __recvMsgThread, NULL); + + return 0; +} diff --git a/build/Makefile.lib.cross b/build/Makefile.lib.cross index 5e2747e..d56abe5 100644 --- a/build/Makefile.lib.cross +++ b/build/Makefile.lib.cross @@ -46,6 +46,7 @@ PLAT_R16_SRCS = \ Fifo/fifo.c \ Skins/skins.c \ Skins/skin_res_vtbl.c \ + IoT/Boardlink/boardlink_iot.c \ Monitor/monitor.c \ log.c \ SvrManager/svr_manager.c \ diff --git a/include/boardlink_iot.h b/include/boardlink_iot.h new file mode 100644 index 0000000..61aacb3 --- /dev/null +++ b/include/boardlink_iot.h @@ -0,0 +1,27 @@ +#ifndef BOARDLINK_IOT_H +#define BOARDLINK_IOT_H + +#define BL_IOT_MSG_TAGS ("BLNLINK") +#define BL_MAX_MSG (1024) + +typedef enum +{ + MSG_IN_DISCOVERY_MODE = 0, + MSG_OUT_DISCOVERY_MODE, + MSG_BYPASS_MODE, +} BL_IOT_MSG_TYPE; + +#pragma pack (push) +#pragma pack (1) +typedef struct +{ + unsigned char msgTags[8]; + unsigned char msgType; + unsigned int msglen; + unsigned char msgData[BL_MAX_MSG]; +} BL_IOT_MSG, *PBL_IOT_MSG; +#pragma pack (pop) + +int BL_Init(void); +int BL_SendMessage(unsigned char* pData, unsigned int len); +#endif diff --git a/include/smart_sound.h b/include/smart_sound.h index 5647275..20d01f0 100644 --- a/include/smart_sound.h +++ b/include/smart_sound.h @@ -433,6 +433,9 @@ typedef enum { ERR_NETWORK_SEND, ERR_NETWORK_NOT_CONNECTED, + ERR_CREATE_SOCKET, + ERR_BIND_SOCKET, + ERR_UNSUPPORT, } DBUS_WITH_LIBUV_ERROR;