Skip to content

Commit 4512c4d

Browse files
Deomid Ryabkovcesantabot
authored andcommitted
ubuntu: More fixes to get demo-c to build and work
cc: @pimvanpelt CL: ubuntu: More fixes to get demo-c to build and work PUBLISHED_FROM=e138bdf87a954b564b48399841f2c6770e4a8ad7
1 parent ce2709a commit 4512c4d

File tree

5 files changed

+88
-49
lines changed

5 files changed

+88
-49
lines changed

fw/platforms/cc32xx/src/cc32xx_gpio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,8 @@ const char *mgos_gpio_str(int pin_def, char buf[8]) {
278278
buf[i++] = '0' + (pin_def / 10);
279279
buf[i++] = '0' + (pin_def % 10);
280280
}
281+
} else {
282+
buf[i++] = '-';
281283
}
282284
buf[i++] = '\0';
283285
return buf;

fw/platforms/esp8266/src/esp_gpio.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,8 @@ const char *mgos_gpio_str(int pin_def, char buf[8]) {
273273
buf[i++] = '1';
274274
buf[i++] = '0' + (pin_def - 10);
275275
}
276+
} else {
277+
buf[i++] = '-';
276278
}
277279
buf[i++] = '\0';
278280
return buf;

fw/platforms/ubuntu/src/ubuntu_gpio.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,12 @@ bool mgos_gpio_blink(int pin, int on_ms, int off_ms) {
265265
return res;
266266
}
267267

268+
const char *mgos_gpio_str(int pin_def, char buf[8]) {
269+
snprintf(buf, 8, "%d", pin_def);
270+
buf[7] = '\0';
271+
return buf;
272+
}
273+
268274
enum mgos_init_result mgos_gpio_init() {
269275
s_lock = mgos_rlock_create();
270276
return mgos_gpio_hal_init();

fw/platforms/ubuntu/src/ubuntu_hal_system.c

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,49 +34,34 @@ struct ubuntu_wdt {
3434

3535
static struct ubuntu_wdt s_mgos_wdt;
3636

37-
static pthread_mutex_t s_mgos_mux = PTHREAD_MUTEX_INITIALIZER;
38-
39-
void mgos_lock(void) {
40-
pthread_mutex_lock(&s_mgos_mux);
41-
return;
42-
}
43-
44-
void mgos_unlock(void) {
45-
pthread_mutex_unlock(&s_mgos_mux);
46-
return;
47-
}
37+
struct mgos_rlock_type {
38+
pthread_mutex_t m;
39+
pthread_mutexattr_t ma;
40+
};
4841

4942
struct mgos_rlock_type *mgos_rlock_create(void) {
50-
pthread_mutex_t *l = calloc(1, sizeof(pthread_mutex_t));
51-
52-
pthread_mutex_init(l, NULL);
53-
return (struct mgos_rlock_type *) l;
43+
struct mgos_rlock_type *l = (struct mgos_rlock_type *) calloc(1, sizeof(*l));
44+
pthread_mutexattr_init(&l->ma);
45+
pthread_mutexattr_settype(&l->ma, PTHREAD_MUTEX_RECURSIVE);
46+
pthread_mutex_init(&l->m, &l->ma);
47+
return l;
5448
}
5549

5650
void mgos_rlock(struct mgos_rlock_type *l) {
57-
if (!l) {
58-
return;
59-
}
60-
pthread_mutex_lock((pthread_mutex_t *) l);
61-
return;
51+
if (l == NULL) return;
52+
pthread_mutex_lock(&l->m);
6253
}
6354

6455
void mgos_runlock(struct mgos_rlock_type *l) {
65-
if (!l) {
66-
return;
67-
}
68-
69-
pthread_mutex_unlock((pthread_mutex_t *) l);
70-
return;
56+
if (l == NULL) return;
57+
pthread_mutex_unlock(&l->m);
7158
}
7259

7360
void mgos_rlock_destroy(struct mgos_rlock_type *l) {
74-
if (!l) {
75-
return;
76-
}
77-
pthread_mutex_destroy((pthread_mutex_t *) l);
61+
if (l == NULL) return;
62+
pthread_mutex_destroy(&l->m);
63+
pthread_mutexattr_destroy(&l->ma);
7864
free(l);
79-
return;
8065
}
8166

8267
size_t mgos_get_heap_size(void) {
@@ -203,15 +188,6 @@ void mgos_ints_enable(void) {
203188
return;
204189
}
205190

206-
bool mgos_invoke_cb(mgos_cb_t cb, void *arg, bool from_isr) {
207-
// LOG(LL_INFO, ("Not implemented"));
208-
return true;
209-
210-
(void) cb;
211-
(void) arg;
212-
(void) from_isr;
213-
}
214-
215191
uint32_t mgos_get_cpu_freq(void) {
216192
int fd = ubuntu_ipc_open("/proc/cpuinfo", O_RDONLY);
217193
char *p;

fw/platforms/ubuntu/src/ubuntu_main.c

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include <sys/wait.h>
1818

19+
#include "common/queue.h"
20+
1921
#include "mgos_debug_internal.h"
2022
#include "mgos_init_internal.h"
2123
#include "mgos_mongoose.h"
@@ -34,9 +36,22 @@ struct ubuntu_flags Flags;
3436
static bool mongoose_running = false;
3537
static pid_t s_parent, s_child;
3638

39+
struct cb_info {
40+
void (*cb)(void *arg);
41+
void *cb_arg;
42+
STAILQ_ENTRY(cb_info) next;
43+
};
44+
45+
STAILQ_HEAD(s_cbs, cb_info) s_cbs = STAILQ_HEAD_INITIALIZER(s_cbs);
46+
struct mgos_rlock_type *s_cbs_lock = NULL;
47+
struct mgos_rlock_type *s_mgos_lock = NULL;
48+
3749
static int ubuntu_mongoose(void) {
3850
enum mgos_init_result r;
3951

52+
s_cbs_lock = mgos_rlock_create();
53+
s_mgos_lock = mgos_rlock_create();
54+
4055
ubuntu_set_boottime();
4156
ubuntu_set_nsleep100();
4257
if (!ubuntu_cap_init()) {
@@ -51,11 +66,33 @@ static int ubuntu_mongoose(void) {
5166
}
5267
mongoose_running = true;
5368
while (mongoose_running) {
54-
mongoose_poll(1000);
69+
mgos_rlock(s_cbs_lock);
70+
while (!STAILQ_EMPTY(&s_cbs)) {
71+
struct cb_info *cbi = STAILQ_FIRST(&s_cbs);
72+
STAILQ_REMOVE_HEAD(&s_cbs, next);
73+
mgos_runlock(s_cbs_lock);
74+
cbi->cb(cbi->cb_arg);
75+
free(cbi);
76+
mgos_rlock(s_cbs_lock);
77+
}
78+
mgos_runlock(s_cbs_lock);
79+
mongoose_poll(1);
5580
}
5681
return 0;
5782
}
5883

84+
bool mgos_invoke_cb(mgos_cb_t cb, void *arg, bool from_isr) {
85+
struct cb_info *cbi = (struct cb_info *) calloc(1, sizeof(*cbi));
86+
if (cbi == NULL) return false;
87+
cbi->cb = cb;
88+
cbi->cb_arg = arg;
89+
mgos_rlock(s_cbs_lock);
90+
STAILQ_INSERT_TAIL(&s_cbs, cbi, next);
91+
mgos_runlock(s_cbs_lock);
92+
(void) from_isr;
93+
return true;
94+
}
95+
5996
static int ubuntu_main(void) {
6097
for (;;) {
6198
int wstatus;
@@ -126,12 +163,33 @@ void mongoose_schedule_poll(bool from_isr) {
126163
(void) from_isr;
127164
}
128165

166+
static void ubuntu_net_up(void *arg) {
167+
struct mgos_net_ip_info ipaddr;
168+
char ip[INET_ADDRSTRLEN], netmask[INET_ADDRSTRLEN], gateway[INET_ADDRSTRLEN];
169+
mgos_eth_dev_get_ip_info(0, &ipaddr);
170+
inet_ntop(AF_INET, (void *) &ipaddr.gw.sin_addr, gateway, INET_ADDRSTRLEN);
171+
inet_ntop(AF_INET, (void *) &ipaddr.ip.sin_addr, ip, INET_ADDRSTRLEN);
172+
inet_ntop(AF_INET, (void *) &ipaddr.netmask.sin_addr, netmask,
173+
INET_ADDRSTRLEN);
174+
LOG(LL_INFO, ("Network: ip=%s netmask=%s gateway=%s", ip, netmask, gateway));
175+
mgos_event_trigger(MGOS_NET_EV_CONNECTING, NULL);
176+
mgos_event_trigger(MGOS_NET_EV_CONNECTED, NULL);
177+
mgos_event_trigger(MGOS_NET_EV_IP_ACQUIRED, NULL);
178+
(void) arg;
179+
}
180+
181+
void mgos_lock(void) {
182+
mgos_rlock(s_mgos_lock);
183+
}
184+
185+
void mgos_unlock(void) {
186+
mgos_runlock(s_mgos_lock);
187+
}
188+
129189
enum mgos_init_result mongoose_init(void) {
130190
enum mgos_init_result r;
131191
int cpu_freq;
132192
size_t heap_size, free_heap_size;
133-
struct mgos_net_ip_info ipaddr;
134-
char ip[INET_ADDRSTRLEN], netmask[INET_ADDRSTRLEN], gateway[INET_ADDRSTRLEN];
135193

136194
r = mgos_uart_init();
137195
if (r != MGOS_INIT_OK) {
@@ -165,12 +223,7 @@ enum mgos_init_result mongoose_init(void) {
165223
LOG(LL_INFO, ("CPU: %d MHz, heap: %lu total, %lu free", cpu_freq, heap_size,
166224
free_heap_size));
167225

168-
mgos_eth_dev_get_ip_info(0, &ipaddr);
169-
inet_ntop(AF_INET, (void *) &ipaddr.gw.sin_addr, gateway, INET_ADDRSTRLEN);
170-
inet_ntop(AF_INET, (void *) &ipaddr.ip.sin_addr, ip, INET_ADDRSTRLEN);
171-
inet_ntop(AF_INET, (void *) &ipaddr.netmask.sin_addr, netmask,
172-
INET_ADDRSTRLEN);
173-
LOG(LL_INFO, ("Network: ip=%s netmask=%s gateway=%s", ip, netmask, gateway));
226+
mgos_invoke_cb(ubuntu_net_up, NULL, false /* from_isr */);
174227

175228
return mgos_init();
176229
}

0 commit comments

Comments
 (0)