add linux board project
This commit is contained in:
104
board/Linux_Posix/mqtt_demo/src/main.c
Normal file
104
board/Linux_Posix/mqtt_demo/src/main.c
Normal file
@@ -0,0 +1,104 @@
|
||||
#include "cmsis_os.h"
|
||||
#include "socket_wrapper.h"
|
||||
#include "sal_module_wrapper.h"
|
||||
#include "mqtt_wrapper.h"
|
||||
#include "mqtt_config.h"
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
int sock_id = 0;
|
||||
|
||||
//mqtt_publisher
|
||||
#define MQTT_PUBLISHER_STK_SIZE 1024
|
||||
void mqtt_publisher(void *pdata);
|
||||
osThreadDef(mqtt_publisher, osPriorityNormal, 1, MQTT_PUBLISHER_STK_SIZE);
|
||||
|
||||
//mqtt_reciever
|
||||
#define MQTT_RECIEVER_STK_SIZE 1024
|
||||
void mqtt_reciever(void *pdata);
|
||||
osThreadDef(mqtt_reciever, osPriorityNormal, 1, MQTT_RECIEVER_STK_SIZE);
|
||||
|
||||
void mqtt_publisher(void *pdata)
|
||||
{
|
||||
mqtt_con_opt_t con_param;
|
||||
con_param.keep_alive_interval = 2000;
|
||||
con_param.cleansession = 1;
|
||||
con_param.username = MQTT_USR_NAME;
|
||||
con_param.password = MQTT_PASSWORD;
|
||||
con_param.client_id = MQTT_CLIENT_ID;
|
||||
|
||||
mqtt_pub_opt_t pub_param;
|
||||
pub_param.dup = 0;
|
||||
pub_param.qos = 0;
|
||||
pub_param.retained = 0;
|
||||
pub_param.id = 0;
|
||||
pub_param.payload = "hello tencent cloud";
|
||||
pub_param.payload_len = 20;
|
||||
pub_param.topic = MQTT_PUBLISH_TOPIC;
|
||||
|
||||
mqtt_sub_opt_t sub_param;
|
||||
sub_param.count = 1;
|
||||
sub_param.dup = 0;
|
||||
sub_param.id = 0;
|
||||
sub_param.req_qos = 0;
|
||||
sub_param.topic = MQTT_SUBSCRIBE_TOPIC;
|
||||
|
||||
printf("start connect\n");
|
||||
tos_sal_module_register(get_socket_module());
|
||||
tos_sal_module_init();
|
||||
|
||||
sock_id = tos_mqtt_connect(MQTT_SERVER_IP, MQTT_SERVER_PORT, &con_param);
|
||||
if (sock_id == -1)
|
||||
{
|
||||
printf("connect failed!!!\n");
|
||||
return -1; //to exit thread
|
||||
}
|
||||
printf("connect success\n");
|
||||
|
||||
if (tos_mqtt_subscribe(sock_id, &sub_param) != 0)
|
||||
{
|
||||
printf("subscribe failed!!!\n");
|
||||
}else{
|
||||
printf("subscribe success\n");
|
||||
}
|
||||
|
||||
osThreadCreate(osThread(mqtt_reciever), NULL); // start receive
|
||||
|
||||
for (;;)
|
||||
{
|
||||
printf("\n");
|
||||
printf("publish topic-->%s| data-->%s| \n", pub_param.topic, pub_param.payload);
|
||||
if (tos_mqtt_publish(sock_id, &pub_param) != 0) {
|
||||
printf("publish failed!!!\n");
|
||||
}
|
||||
osDelay(2000);
|
||||
}
|
||||
}
|
||||
|
||||
void mqtt_reciever(void *pdata)
|
||||
{
|
||||
uint8_t read_data[100];
|
||||
int8_t topic[30];
|
||||
uint32_t read_len;
|
||||
|
||||
for (;;)
|
||||
{
|
||||
read_len = tos_mqtt_receive(topic, sizeof(topic), read_data, sizeof(read_data));
|
||||
if (read_len >= 0)
|
||||
{
|
||||
printf("receive topic-->%s| data-->%s| \n", topic, read_data);
|
||||
}
|
||||
osDelay(100);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
osKernelInitialize(); //TOS Tiny kernel initialize
|
||||
osThreadCreate(osThread(mqtt_publisher), NULL); // start connect and publish
|
||||
osKernelStart(); //Start TOS Tiny
|
||||
|
||||
while (1)
|
||||
{
|
||||
}
|
||||
}
|
143
board/Linux_Posix/mqtt_demo/src/socket_wrapper.c
Normal file
143
board/Linux_Posix/mqtt_demo/src/socket_wrapper.c
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "socket_wrapper.h"
|
||||
#include "cmsis_os.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/socket.h>
|
||||
#include <netinet/in.h>
|
||||
#include <signal.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
static osMutexId socket_send_lock, socket_recv_lock;
|
||||
|
||||
osMutexDef(socket_send_lock);
|
||||
osMutexDef(socket_recv_lock);
|
||||
|
||||
static osSemaphoreId socket_recv_event;
|
||||
osSemaphoreDef(socket_recv_event);
|
||||
|
||||
int socket_init(void)
|
||||
{
|
||||
socket_send_lock = osMutexCreate(osMutex(socket_send_lock));
|
||||
socket_recv_lock = osMutexCreate(osMutex(socket_recv_lock));
|
||||
socket_recv_event = osSemaphoreCreate(osSemaphore(socket_recv_event),0);
|
||||
|
||||
return (
|
||||
(socket_recv_event != NULL) &&
|
||||
(socket_recv_lock != NULL) &&
|
||||
(socket_send_lock != NULL)
|
||||
);
|
||||
}
|
||||
|
||||
void io_signal_handle(int signal)
|
||||
{
|
||||
osSemaphoreRelease(socket_recv_event);
|
||||
}
|
||||
|
||||
int socket_connect(const char *ip, const char *port, sal_proto_t proto)
|
||||
{
|
||||
struct sockaddr_in addr = {
|
||||
.sin_family = AF_INET,
|
||||
.sin_port = htons(atoi(port))};
|
||||
int socket_proto = 0;
|
||||
struct sigaction sig_install;
|
||||
|
||||
inet_pton(AF_INET, ip, &addr.sin_addr);
|
||||
|
||||
if (TOS_SAL_PROTO_TCP == proto)
|
||||
{
|
||||
socket_proto = IPPROTO_TCP;
|
||||
}
|
||||
else if (TOS_SAL_PROTO_UDP == proto)
|
||||
{
|
||||
socket_proto = IPPROTO_UDP;
|
||||
}
|
||||
else
|
||||
{
|
||||
return -1;
|
||||
}
|
||||
|
||||
int socket_id = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
|
||||
fcntl( socket_id, __F_SETOWN, getpid() );
|
||||
fcntl( socket_id, __F_SETSIG, SIGIO );
|
||||
fcntl( socket_id, F_SETFL, O_ASYNC );
|
||||
|
||||
sig_install.sa_flags = 0;
|
||||
sig_install.sa_handler = io_signal_handle;
|
||||
sigfillset( &sig_install.sa_mask );
|
||||
|
||||
if ( 0 != sigaction( SIGIO, &sig_install, NULL ) )
|
||||
{
|
||||
printf( "socket problem installing %d\n",SIGIO );
|
||||
}
|
||||
|
||||
connect(socket_id, &addr, sizeof(addr));
|
||||
return socket_id;
|
||||
}
|
||||
|
||||
int socket_send(int sock, const void *buf, size_t len)
|
||||
{
|
||||
ssize_t send_len = 0, state;
|
||||
if (sock < 0)
|
||||
return -1;
|
||||
osMutexWait(socket_send_lock, TOS_TIME_FOREVER);
|
||||
do
|
||||
{
|
||||
state = send(sock, (buf + send_len), (len - send_len), MSG_DONTWAIT);
|
||||
if (state > 0)
|
||||
{
|
||||
send_len += state;
|
||||
}
|
||||
if (send_len != len)
|
||||
{
|
||||
osSemaphoreWait(socket_recv_event,100);
|
||||
}
|
||||
} while (len != send_len);
|
||||
osMutexRelease(socket_send_lock);
|
||||
return send_len;
|
||||
}
|
||||
|
||||
int socket_recv(int sock, void *buf, size_t len)
|
||||
{
|
||||
ssize_t recv_len = 0, state;
|
||||
if (sock < 0)
|
||||
return -1;
|
||||
osMutexWait(socket_recv_lock, TOS_TIME_FOREVER);
|
||||
do
|
||||
{
|
||||
state = recv(sock, (buf + recv_len), (len - recv_len), MSG_DONTWAIT);
|
||||
if (state > 0)
|
||||
{
|
||||
recv_len += state;
|
||||
}
|
||||
if (recv_len != len)
|
||||
{
|
||||
osSemaphoreWait(socket_recv_event,100);
|
||||
}
|
||||
} while (len != recv_len);
|
||||
osMutexRelease(socket_recv_lock);
|
||||
return recv_len;
|
||||
}
|
||||
|
||||
int socket_close(int sock)
|
||||
{
|
||||
close(sock);
|
||||
}
|
||||
|
||||
static sal_module_t linux_sal = {
|
||||
.init = socket_init,
|
||||
.connect = socket_connect,
|
||||
.send = socket_send,
|
||||
.recv = socket_recv,
|
||||
.close = socket_close,
|
||||
// .sendto = NULL,
|
||||
// .recv_timeout = NULL,
|
||||
// .recvfrom = NULL,
|
||||
// .recvfrom_timeout = NULL,
|
||||
// .parse_domain = NULL,
|
||||
};
|
||||
|
||||
sal_module_t *get_socket_module(void)
|
||||
{
|
||||
return (&linux_sal);
|
||||
}
|
Reference in New Issue
Block a user