Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions class/a9g/at_socket_a9g.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops a9g_socket_ops =
{
RT_NULL,
a9g_socket_connect,
a9g_socket_close,
a9g_socket_send,
Expand Down
13 changes: 7 additions & 6 deletions class/air720/at_socket_air720.c
Original file line number Diff line number Diff line change
Expand Up @@ -645,12 +645,13 @@ static const struct at_urc urc_table[] =
};

static const struct at_socket_ops air720_socket_ops =
{
air720_socket_connect,
air720_socket_close,
air720_socket_send,
air720_domain_resolve,
air720_socket_set_event_cb,
{
RT_NULL,
air720_socket_connect,
air720_socket_close,
air720_socket_send,
air720_domain_resolve,
air720_socket_set_event_cb,
};

int air720_socket_init(struct at_device *device)
Expand Down
1 change: 1 addition & 0 deletions class/bc26/at_socket_bc26.c
Original file line number Diff line number Diff line change
Expand Up @@ -759,6 +759,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops bc26_socket_ops =
{
RT_NULL,
bc26_socket_connect,
bc26_socket_close,
bc26_socket_send,
Expand Down
15 changes: 15 additions & 0 deletions class/bc28/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# BC28

## 注意事项

### 申请 socket

- BC28 最多支持 7 个 socket,编号 0-6;
- 如果 BC28 使用支持 MQTT 或 CoAP 协议的固件,则相关服务会占用部分 socket,因此最小可用的 socket 并不一定是 0;
- 可以创建 1 个 UDP socket,多个 TCP socket;
- 另外,执行 DNS 域名解析时也会占用 socket;

### 连接 socket

- R02 的基线版本固件才支持 `AT+QTCPIND` 查询 TCP 连接情况,因此目前采用保守的 30 秒延时来确保连接状态;

46 changes: 11 additions & 35 deletions class/bc28/at_socket_bc28.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* Change Logs:
* Date Author Notes
* 2020-02-13 luhuadong first version
* 2020-07-19 luhuadong support alloc socket
*/

#include <stdio.h>
Expand Down Expand Up @@ -194,7 +195,7 @@ static int bc28_socket_close(struct at_socket *socket)
{
int result = RT_EOK;
at_response_t resp = RT_NULL;
int device_socket = (int) socket->user_data + AT_DEVICE_BC28_MIN_SOCKET;
int device_socket = (int) socket->user_data;
struct at_device *device = (struct at_device *) socket->device;

resp = at_create_resp(64, 0, rt_tick_from_millisecond(3000));
Expand Down Expand Up @@ -223,16 +224,16 @@ static int bc28_socket_close(struct at_socket *socket)
* create socket by AT commands.
*
* @param type connect socket type(tcp, udp)
* @param port listen port (range: 0-65535), if 0 means get a random port
*
* @return >=0: create socket success, return the socket id (0-6)
* -1: send or exec AT commands error
* -5: no memory
*/
static int bc28_socket_create(struct at_device *device, enum at_socket_type type, uint32_t port)
static int bc28_socket_create(struct at_device *device, enum at_socket_type type)
{
const char *type_str = RT_NULL;
uint32_t protocol = 0;
uint32_t port = 0; /* range: 0-65535, if 0 means get a random port */
at_response_t resp = RT_NULL;
int socket = -1, result = 0;

Expand Down Expand Up @@ -309,7 +310,7 @@ static int bc28_socket_connect(struct at_socket *socket, char *ip, int32_t port,
uint32_t event = 0;
at_response_t resp = RT_NULL;
int result = 0, event_result = 0;
int device_socket = (int) socket->user_data + AT_DEVICE_BC28_MIN_SOCKET;
int device_socket = (int) socket->user_data;
int return_socket = -1;
struct at_device *device = (struct at_device *) socket->device;

Expand All @@ -328,32 +329,6 @@ static int bc28_socket_connect(struct at_socket *socket, char *ip, int32_t port,
return -RT_ENOMEM;
}

return_socket = bc28_socket_create(device, type, 0);

if (return_socket != device_socket)
{
LOG_E("socket not match (request %d, return %d).", device_socket, return_socket);

result = at_obj_exec_cmd(device->client, resp, "AT+NSOCL=%d", return_socket);
if (result < 0)
{
LOG_E("%s device close socket(%d) failed [%d].", device->name, return_socket, result);
}
else
{
LOG_D("%s device close socket(%d).", device->name, return_socket);

/* notice the socket is disconnect by remote */
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
{
at_evt_cb_set[AT_SOCKET_EVT_CLOSED](socket, AT_SOCKET_EVT_CLOSED, NULL, 0);
}
}

result = -RT_ERROR;
goto __exit;
}

/* if the protocol is not tcp, no need connect to server */
if (type != AT_SOCKET_TCP)
{
Expand Down Expand Up @@ -439,7 +414,7 @@ static int bc28_socket_send(struct at_socket *socket, const char *buff,
int result = 0, event_result = 0;
size_t cur_pkt_size = 0, sent_size = 0;
at_response_t resp = RT_NULL;
int device_socket = (int) socket->user_data + AT_DEVICE_BC28_MIN_SOCKET;
int device_socket = (int) socket->user_data;
struct at_device *device = (struct at_device *) socket->device;
struct at_device_bc28 *bc28 = (struct at_device_bc28 *) device->user_data;
rt_mutex_t lock = device->client->lock;
Expand Down Expand Up @@ -748,10 +723,10 @@ static void urc_close_func(struct at_client *client, const char *data, rt_size_t

bc28_socket_event_send(device, SET_EVENT(device_socket, BC28_EVENT_CONN_FAIL));

if (device_socket - AT_DEVICE_BC28_MIN_SOCKET >= 0)
if (device_socket >= 0)
{
/* get at socket object by device socket descriptor */
socket = &(device->sockets[device_socket - AT_DEVICE_BC28_MIN_SOCKET]);
socket = &(device->sockets[device_socket]);

/* notice the socket is disconnect by remote */
if (at_evt_cb_set[AT_SOCKET_EVT_CLOSED])
Expand Down Expand Up @@ -828,7 +803,7 @@ static void urc_recv_func(struct at_client *client, const char *data, rt_size_t
rt_free(hex_buf);

/* get at socket object by device socket descriptor */
socket = &(device->sockets[device_socket - AT_DEVICE_BC28_MIN_SOCKET]);
socket = &(device->sockets[device_socket]);

/* notice the receive buffer and buffer size */
if (at_evt_cb_set[AT_SOCKET_EVT_RECV])
Expand Down Expand Up @@ -895,6 +870,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops bc28_socket_ops =
{
bc28_socket_create,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

建议添加 AT 组件版本号判断已经将 bc28_socket_create 定义放在结构体最后,以提高版本兼容性。

详见 RT-Thread/rt-thread#3755 PR 说明

bc28_socket_connect,
bc28_socket_close,
bc28_socket_send,
Expand All @@ -916,7 +892,7 @@ int bc28_socket_class_register(struct at_device_class *class)
{
RT_ASSERT(class);

class->socket_num = AT_DEVICE_BC28_SOCKETS_NUM - AT_DEVICE_BC28_MIN_SOCKET;
class->socket_num = AT_DEVICE_BC28_SOCKETS_NUM;
class->socket_ops = &bc28_socket_ops;

return RT_EOK;
Expand Down
1 change: 1 addition & 0 deletions class/ec20/at_socket_ec20.c
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops ec20_socket_ops =
{
RT_NULL,
ec20_socket_connect,
ec20_socket_close,
ec20_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/ec200x/at_socket_ec200x.c
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops ec200x_socket_ops =
{
RT_NULL,
ec200x_socket_connect,
ec200x_socket_close,
ec200x_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/esp32/at_socket_esp32.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ static void esp32_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)

static const struct at_socket_ops esp32_socket_ops =
{
RT_NULL,
esp32_socket_connect,
esp32_socket_close,
esp32_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/esp8266/at_socket_esp8266.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,7 @@ static void esp8266_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)

static const struct at_socket_ops esp8266_socket_ops =
{
RT_NULL,
esp8266_socket_connect,
esp8266_socket_close,
esp8266_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/m26/at_socket_m26.c
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops m26_socket_ops =
{
RT_NULL,
m26_socket_connect,
m26_socket_close,
m26_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/m5311/at_socket_m5311.c
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops m5311_socket_ops =
{
RT_NULL,
m5311_socket_connect,
m5311_socket_close,
m5311_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/m6315/at_socket_m6315.c
Original file line number Diff line number Diff line change
Expand Up @@ -606,6 +606,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops m6315_socket_ops =
{
RT_NULL,
m6315_socket_connect,
m6315_socket_close,
m6315_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/me3616/at_socket_me3616.c
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops me3616_socket_ops =
{
RT_NULL,
me3616_socket_connect,
me3616_socket_close,
me3616_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/mw31/at_socket_mw31.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ static void mw31_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)

static const struct at_socket_ops mw31_socket_ops =
{
RT_NULL,
mw31_socket_connect,
mw31_socket_close,
mw31_socket_send,
Expand Down
13 changes: 7 additions & 6 deletions class/n21/at_socket_n21.c
Original file line number Diff line number Diff line change
Expand Up @@ -630,12 +630,13 @@ static const struct at_urc urc_table[] =
};

static const struct at_socket_ops n21_socket_ops =
{
n21_socket_connect,
n21_socket_close,
n21_socket_send,
n21_domain_resolve,
n21_socket_set_event_cb,
{
RT_NULL,
n21_socket_connect,
n21_socket_close,
n21_socket_send,
n21_domain_resolve,
n21_socket_set_event_cb,
};

int n21_socket_init(struct at_device *device)
Expand Down
13 changes: 7 additions & 6 deletions class/n58/at_socket_n58.c
Original file line number Diff line number Diff line change
Expand Up @@ -638,12 +638,13 @@ static const struct at_urc urc_table[] =
};

static const struct at_socket_ops n58_socket_ops =
{
n58_socket_connect,
n58_socket_close,
n58_socket_send,
n58_domain_resolve,
n58_socket_set_event_cb,
{
RT_NULL,
n58_socket_connect,
n58_socket_close,
n58_socket_send,
n58_domain_resolve,
n58_socket_set_event_cb,
};

int n58_socket_init(struct at_device *device)
Expand Down
1 change: 1 addition & 0 deletions class/rw007/at_socket_rw007.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,7 @@ static void rw007_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)

static const struct at_socket_ops rw007_socket_ops =
{
RT_NULL,
rw007_socket_connect,
rw007_socket_close,
rw007_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/sim76xx/at_socket_sim76xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,7 @@ static struct at_urc urc_table[] =

static const struct at_socket_ops sim76xx_socket_ops =
{
RT_NULL,
sim76xx_socket_connect,
sim76xx_socket_close,
sim76xx_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/sim800c/at_socket_sim800c.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,7 @@ static const struct at_urc urc_table[] =

static const struct at_socket_ops sim800c_socket_ops =
{
RT_NULL,
sim800c_socket_connect,
sim800c_socket_close,
sim800c_socket_send,
Expand Down
1 change: 1 addition & 0 deletions class/w60x/at_socket_w60x.c
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ static void w60x_socket_set_event_cb(at_socket_evt_t event, at_evt_cb_t cb)

static const struct at_socket_ops w60x_socket_ops =
{
RT_NULL,
w60x_socket_connect,
w60x_socket_close,
w60x_socket_send,
Expand Down