Skip to content

Commit

Permalink
ubuntu: More fixes to get demo-c to build and work
Browse files Browse the repository at this point in the history
cc: @pimvanpelt

CL: ubuntu: More fixes to get demo-c to build and work

PUBLISHED_FROM=e138bdf87a954b564b48399841f2c6770e4a8ad7
  • Loading branch information
Deomid Ryabkov authored and cesantabot committed Feb 19, 2019
1 parent ce2709a commit 4512c4d
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 49 deletions.
2 changes: 2 additions & 0 deletions fw/platforms/cc32xx/src/cc32xx_gpio.c
Expand Up @@ -278,6 +278,8 @@ const char *mgos_gpio_str(int pin_def, char buf[8]) {
buf[i++] = '0' + (pin_def / 10);
buf[i++] = '0' + (pin_def % 10);
}
} else {
buf[i++] = '-';
}
buf[i++] = '\0';
return buf;
Expand Down
2 changes: 2 additions & 0 deletions fw/platforms/esp8266/src/esp_gpio.c
Expand Up @@ -273,6 +273,8 @@ const char *mgos_gpio_str(int pin_def, char buf[8]) {
buf[i++] = '1';
buf[i++] = '0' + (pin_def - 10);
}
} else {
buf[i++] = '-';
}
buf[i++] = '\0';
return buf;
Expand Down
6 changes: 6 additions & 0 deletions fw/platforms/ubuntu/src/ubuntu_gpio.c
Expand Up @@ -265,6 +265,12 @@ bool mgos_gpio_blink(int pin, int on_ms, int off_ms) {
return res;
}

const char *mgos_gpio_str(int pin_def, char buf[8]) {
snprintf(buf, 8, "%d", pin_def);
buf[7] = '\0';
return buf;
}

enum mgos_init_result mgos_gpio_init() {
s_lock = mgos_rlock_create();
return mgos_gpio_hal_init();
Expand Down
56 changes: 16 additions & 40 deletions fw/platforms/ubuntu/src/ubuntu_hal_system.c
Expand Up @@ -34,49 +34,34 @@ struct ubuntu_wdt {

static struct ubuntu_wdt s_mgos_wdt;

static pthread_mutex_t s_mgos_mux = PTHREAD_MUTEX_INITIALIZER;

void mgos_lock(void) {
pthread_mutex_lock(&s_mgos_mux);
return;
}

void mgos_unlock(void) {
pthread_mutex_unlock(&s_mgos_mux);
return;
}
struct mgos_rlock_type {
pthread_mutex_t m;
pthread_mutexattr_t ma;
};

struct mgos_rlock_type *mgos_rlock_create(void) {
pthread_mutex_t *l = calloc(1, sizeof(pthread_mutex_t));

pthread_mutex_init(l, NULL);
return (struct mgos_rlock_type *) l;
struct mgos_rlock_type *l = (struct mgos_rlock_type *) calloc(1, sizeof(*l));
pthread_mutexattr_init(&l->ma);
pthread_mutexattr_settype(&l->ma, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&l->m, &l->ma);
return l;
}

void mgos_rlock(struct mgos_rlock_type *l) {
if (!l) {
return;
}
pthread_mutex_lock((pthread_mutex_t *) l);
return;
if (l == NULL) return;
pthread_mutex_lock(&l->m);
}

void mgos_runlock(struct mgos_rlock_type *l) {
if (!l) {
return;
}

pthread_mutex_unlock((pthread_mutex_t *) l);
return;
if (l == NULL) return;
pthread_mutex_unlock(&l->m);
}

void mgos_rlock_destroy(struct mgos_rlock_type *l) {
if (!l) {
return;
}
pthread_mutex_destroy((pthread_mutex_t *) l);
if (l == NULL) return;
pthread_mutex_destroy(&l->m);
pthread_mutexattr_destroy(&l->ma);
free(l);
return;
}

size_t mgos_get_heap_size(void) {
Expand Down Expand Up @@ -203,15 +188,6 @@ void mgos_ints_enable(void) {
return;
}

bool mgos_invoke_cb(mgos_cb_t cb, void *arg, bool from_isr) {
// LOG(LL_INFO, ("Not implemented"));
return true;

(void) cb;
(void) arg;
(void) from_isr;
}

uint32_t mgos_get_cpu_freq(void) {
int fd = ubuntu_ipc_open("/proc/cpuinfo", O_RDONLY);
char *p;
Expand Down
71 changes: 62 additions & 9 deletions fw/platforms/ubuntu/src/ubuntu_main.c
Expand Up @@ -16,6 +16,8 @@

#include <sys/wait.h>

#include "common/queue.h"

#include "mgos_debug_internal.h"
#include "mgos_init_internal.h"
#include "mgos_mongoose.h"
Expand All @@ -34,9 +36,22 @@ struct ubuntu_flags Flags;
static bool mongoose_running = false;
static pid_t s_parent, s_child;

struct cb_info {
void (*cb)(void *arg);
void *cb_arg;
STAILQ_ENTRY(cb_info) next;
};

STAILQ_HEAD(s_cbs, cb_info) s_cbs = STAILQ_HEAD_INITIALIZER(s_cbs);
struct mgos_rlock_type *s_cbs_lock = NULL;
struct mgos_rlock_type *s_mgos_lock = NULL;

static int ubuntu_mongoose(void) {
enum mgos_init_result r;

s_cbs_lock = mgos_rlock_create();
s_mgos_lock = mgos_rlock_create();

ubuntu_set_boottime();
ubuntu_set_nsleep100();
if (!ubuntu_cap_init()) {
Expand All @@ -51,11 +66,33 @@ static int ubuntu_mongoose(void) {
}
mongoose_running = true;
while (mongoose_running) {
mongoose_poll(1000);
mgos_rlock(s_cbs_lock);
while (!STAILQ_EMPTY(&s_cbs)) {
struct cb_info *cbi = STAILQ_FIRST(&s_cbs);
STAILQ_REMOVE_HEAD(&s_cbs, next);
mgos_runlock(s_cbs_lock);
cbi->cb(cbi->cb_arg);
free(cbi);
mgos_rlock(s_cbs_lock);
}
mgos_runlock(s_cbs_lock);
mongoose_poll(1);
}
return 0;
}

bool mgos_invoke_cb(mgos_cb_t cb, void *arg, bool from_isr) {
struct cb_info *cbi = (struct cb_info *) calloc(1, sizeof(*cbi));
if (cbi == NULL) return false;
cbi->cb = cb;
cbi->cb_arg = arg;
mgos_rlock(s_cbs_lock);
STAILQ_INSERT_TAIL(&s_cbs, cbi, next);
mgos_runlock(s_cbs_lock);
(void) from_isr;
return true;
}

static int ubuntu_main(void) {
for (;;) {
int wstatus;
Expand Down Expand Up @@ -126,12 +163,33 @@ void mongoose_schedule_poll(bool from_isr) {
(void) from_isr;
}

static void ubuntu_net_up(void *arg) {
struct mgos_net_ip_info ipaddr;
char ip[INET_ADDRSTRLEN], netmask[INET_ADDRSTRLEN], gateway[INET_ADDRSTRLEN];
mgos_eth_dev_get_ip_info(0, &ipaddr);
inet_ntop(AF_INET, (void *) &ipaddr.gw.sin_addr, gateway, INET_ADDRSTRLEN);
inet_ntop(AF_INET, (void *) &ipaddr.ip.sin_addr, ip, INET_ADDRSTRLEN);
inet_ntop(AF_INET, (void *) &ipaddr.netmask.sin_addr, netmask,
INET_ADDRSTRLEN);
LOG(LL_INFO, ("Network: ip=%s netmask=%s gateway=%s", ip, netmask, gateway));
mgos_event_trigger(MGOS_NET_EV_CONNECTING, NULL);
mgos_event_trigger(MGOS_NET_EV_CONNECTED, NULL);
mgos_event_trigger(MGOS_NET_EV_IP_ACQUIRED, NULL);
(void) arg;
}

void mgos_lock(void) {
mgos_rlock(s_mgos_lock);
}

void mgos_unlock(void) {
mgos_runlock(s_mgos_lock);
}

enum mgos_init_result mongoose_init(void) {
enum mgos_init_result r;
int cpu_freq;
size_t heap_size, free_heap_size;
struct mgos_net_ip_info ipaddr;
char ip[INET_ADDRSTRLEN], netmask[INET_ADDRSTRLEN], gateway[INET_ADDRSTRLEN];

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

mgos_eth_dev_get_ip_info(0, &ipaddr);
inet_ntop(AF_INET, (void *) &ipaddr.gw.sin_addr, gateway, INET_ADDRSTRLEN);
inet_ntop(AF_INET, (void *) &ipaddr.ip.sin_addr, ip, INET_ADDRSTRLEN);
inet_ntop(AF_INET, (void *) &ipaddr.netmask.sin_addr, netmask,
INET_ADDRSTRLEN);
LOG(LL_INFO, ("Network: ip=%s netmask=%s gateway=%s", ip, netmask, gateway));
mgos_invoke_cb(ubuntu_net_up, NULL, false /* from_isr */);

return mgos_init();
}

0 comments on commit 4512c4d

Please sign in to comment.