Beispiel #1
0
static int8_t loader_handler( void *state, Message *msg )
{
	switch (msg->type){
		case MSG_TIMER_TIMEOUT: 
		{ return handle_timeout( msg ); }

		case MSG_VERSION_ADV:
		{
			msg_version_adv_t *pkt = (msg_version_adv_t *) msg->data;
			pkt->version = version_ntoh(pkt->version);	
			if(pkt->version >= st.version_data->version) {
				st.recent_neighbor = msg->saddr;
			}
			return handle_version_adv( msg ); 
		}

		case MSG_VERSION_DATA:
		{ 
			msg_version_data_t *pkt = (msg_version_data_t *) msg->data;
			pkt->version = version_ntoh(pkt->version);
			if(pkt->version >= st.version_data->version) {
				st.recent_neighbor = msg->saddr;
			}
			return handle_version_data( msg ); 
		}

		case MSG_LOADER_LS_ON_NODE:
		{
			return handle_loader_ls_on_node( msg );
		}

		case MSG_FETCHER_DONE:
		{ 
			return handle_fetcher_done( msg ); 
		}

		case MSG_INIT:
		{ 
#ifdef LOADER_NET_EXPERIMENT
			handle_init();
			if( ker_id() == 0 ) {
				start_experiment(EXPERIMENT_SIZE);	
			}
#else
			return handle_init(); 
#endif
		}

		default:
		return -EINVAL;
	}
	return SOS_OK;
}
Beispiel #2
0
void start_client() {
  status = S_INIT;
  while (true) {
    switch(status) {
      case S_INIT:
        handle_init(); 
        break;
      case S_EXIT:
        handle_exit();
        return;
      case S_LOGIN:
        handle_login();
        break;
      case S_REGISTER:
        handle_register();
        break;
      case S_VERIFIED:
        handle_verified();
        break;
      case S_ORDER:
        handle_order();
        break;
      case S_QUERY_ORDER:
        handle_query_orders();
        break;
      case S_QUERY:
        handle_query();
      default:
        break;
    }
  }
}
int main(void) {
  handle_init();

  app_event_loop();

  handle_deinit();
}
Beispiel #4
0
int crypto_data_init (unsigned min, unsigned max)
{
	if (handles) {	/* Already initialized -- reset. */
		memset (crypto, 0, sizeof (CryptoData_t *) * cur_handles);
		handle_reset (handles);
		return (DDS_RETCODE_OK);
	}

	/* Allocate the crypto handles. */
	handles = handle_init (min);
	if (!handles) {
		warn_printf ("crypto_init: not enough memory for crypto handles!\r\n");
		return (DDS_RETCODE_OUT_OF_RESOURCES);
	}

	/* Initialize the crypto array. */
	cur_handles = min_handles = min;
	max_handles = max;
	crypto = xmalloc ((cur_handles + 1) * sizeof (CryptoData_t *));
	if (!crypto) {
		warn_printf ("crypto_init: not enough memory for crypto table!\r\n");
		handle_final (handles);
		return (DDS_RETCODE_OUT_OF_RESOURCES);
	}
	sec_crypt_alloc = (cur_handles + 1) * sizeof (CryptoData_t *);
	return (DDS_RETCODE_OK);
}
Beispiel #5
0
void init_rpc(void)
{
    GEN_CONFIG_T  t_rtos_config;
    bzero(&t_rtos_config, sizeof(GEN_CONFIG_T));
    void*       pv_mem_addr = 0;
    unsigned int z_mem_size = 0xc00000;
    int ret = 0;

    ret = x_rtos_init(&t_rtos_config);
    if (ret != 0) {
        printf("rtos init failed %d \n", ret);
    }
    ret = handle_init(MAX_NUM_HANDLES, &pv_mem_addr, &z_mem_size);
    if (ret != 0) {
        printf("handle init failed %d \n", ret);
    }
    ret = os_init(pv_mem_addr, z_mem_size);
    if (ret != 0) {
        printf("os init failed %d \n", ret);
    }
    ret = c_rpc_init_client();
    if (ret != 0) {
        printf("rpc init failed %d \n", ret);
    }
    ret = c_rpc_start_client();
    if (ret < 0) {
        printf("rpc start failed %d \n", ret);
    }
    printf("Rpc init OK\n");
}
bool ForeignMenuConnection::read_msgs()
{
    uint8_t *pos = _read_buf;
    size_t nread = _read_pos - _read_buf;
    int32_t size;

    ASSERT(_handler);
    ASSERT(_opaque != NamedPipe::INVALID_CONNECTION);
    size = NamedPipe::read(_opaque, (uint8_t*)_read_pos, sizeof(_read_buf) - nread);
    if (size == 0) {
        return false;
    } else if (size < 0) {
        LOG_ERROR("Error reading from named pipe %d", size);
        _parent.remove_connection(_opaque);
        return false;
    }
    nread += size;
    while (nread > 0) {
        if (!_initialized && nread >= sizeof(FrgMenuInitHeader)) {
            FrgMenuInitHeader *init = (FrgMenuInitHeader *)pos;
            if (init->magic != FOREIGN_MENU_MAGIC || init->version != FOREIGN_MENU_VERSION) {
                LOG_ERROR("Bad foreign menu init, magic=0x%x version=%u", init->magic,
                          init->version);
                _parent.remove_connection(_opaque);
                return false;
            }
            if (nread < init->size) {
                break;
            }
            if (!handle_init((FrgMenuInit*)init)) {
                _parent.remove_connection(_opaque);
                return false;
            }
            nread -= init->size;
            pos += init->size;
            _initialized = true;
        }
        if (!_initialized || nread < sizeof(FrgMenuMsg)) {
            break;
        }
        FrgMenuMsg *hdr = (FrgMenuMsg *)pos;
        if (hdr->size < sizeof(FrgMenuMsg)) {
            LOG_ERROR("Bad foreign menu message, size=%u", hdr->size);
            _parent.remove_connection(_opaque);
            return false;
        }
        if (nread < hdr->size) {
            break;
        }
        handle_message(hdr);
        nread -= hdr->size;
        pos += hdr->size;
    }
    if (nread > 0 && pos != _read_buf) {
        memmove(_read_buf, pos, nread);
    }
    _read_pos = _read_buf + nread;
    return true;
}
Beispiel #7
0
struct ludis_handle *
handle_new()
{
    struct ludis_handle *h;

    h = lmalloc(sizeof(struct ludis_handle));
    handle_init(h);
    return h;
}
Beispiel #8
0
int main(void) {
    srand(time(NULL));
	handle_init();
    /*PebbleAppHandlers handlers = {
        .init_handler = &handle_init,
        .timer_handler = &handle_timeout
    };*/
    app_event_loop();
	handle_deinit();
}
Beispiel #9
0
int main(void) {
    handle_init();
    app_message_init();
    
    //get_events_handler();
    //time(&start);
    
    app_event_loop();
    handle_deinit();
}
Beispiel #10
0
/** Open endpoint protocols. */
static int open_endpoint(struct network *net, struct endpoint *ep, struct sockaddr *sa, uint32_t flags)
{
	if (flags & NET_UDP) {
		handle_init(udp, net->loop, &ep->udp, sa->sa_family);
		int ret = udp_bind(ep, sa);
		if (ret != 0) {
			return ret;
		}
		ep->flags |= NET_UDP;
	}
	if (flags & NET_TCP) {
		handle_init(tcp, net->loop, &ep->tcp, sa->sa_family);
		int ret = tcp_bind(ep, sa);
		if (ret != 0) {
			return ret;
		}
		ep->flags |= NET_TCP;
	}
	return kr_ok();
}
Beispiel #11
0
int main(void)
{
	APP_LOG(APP_LOG_LEVEL_INFO, "RingMyPhone app started");
	
	handle_init();
	app_event_loop();
	handle_deinit();
	
	APP_LOG(APP_LOG_LEVEL_INFO, "RingMyPhone app exiting");
	
	return 0;
}
Beispiel #12
0
int main(void) {
    locale_init();
    
    handle_init();
    app_event_loop();
    
    if(launch_reason() == APP_LAUNCH_TIMELINE_ACTION) {
        //lastIdFromAppLaunch = (int)launch_get_args();
        // Start voice dictation UI
        //dictation_session_start(s_dictation_session);
    }
    
    handle_deinit();
}
Beispiel #13
0
void handle_msi_packet(Messenger *m, uint32_t friend_number, const uint8_t *data, uint16_t length, void *object)
{
    LOGGER_DEBUG(m->log, "Got msi message");

    MSISession *session = (MSISession *)object;
    MSIMessage msg;

    if (msg_parse_in(m->log, &msg, data, length) == -1) {
        LOGGER_WARNING(m->log, "Error parsing message");
        send_error(m, friend_number, msi_EInvalidMessage);
        return;
    }

    LOGGER_DEBUG(m->log, "Successfully parsed message");

    pthread_mutex_lock(session->mutex);
    MSICall *call = get_call(session, friend_number);

    if (call == NULL) {
        if (msg.request.value != requ_init) {
            send_error(m, friend_number, msi_EStrayMessage);
            pthread_mutex_unlock(session->mutex);
            return;
        }

        call = new_call(session, friend_number);

        if (call == NULL) {
            send_error(m, friend_number, msi_ESystem);
            pthread_mutex_unlock(session->mutex);
            return;
        }
    }

    switch (msg.request.value) {
        case requ_init:
            handle_init(call, &msg);
            break;

        case requ_push:
            handle_push(call, &msg);
            break;

        case requ_pop:
            handle_pop(call, &msg); /* always kills the call */
            break;
    }

    pthread_mutex_unlock(session->mutex);
}
Beispiel #14
0
int main(void) {
  if (launch_reason() == APP_LAUNCH_WAKEUP) {
    APP_LOG(APP_LOG_LEVEL_DEBUG, "launch wakeup");
    wakeup_service_subscribe(NULL);
    BatteryChargeState charge_state = battery_state_service_peek();
    save_charge_state(&charge_state);
    schedule_wakeup_measure_battery_state();
    app_event_loop();
  } else {
    handle_init();
    app_event_loop();
    handle_deinit();
  }
}
Beispiel #15
0
void * neon_vfs_fopen_impl (const char * path, const char * mode)
{
    struct neon_handle * handle = handle_init ();

    _DEBUG ("<%p> Trying to open '%s' with neon", (void *) handle, path);

    handle->url = g_strdup (path);

    if (open_handle (handle, 0) != 0)
    {
        _ERROR ("<%p> Could not open URL", (void *) handle);
        handle_free (handle);
        return NULL;
    }

    return handle;
}
void Control_socket_manager::handle_control_message(Control_message* message) {

	set_message_destination_ip(message);

	const int code = message->header.control_code;
	switch (code) {

	case AUTHOR:
		handle_author(message);
		break;
	case INIT:
		handle_init(message);
		break;
	case ROUTING_TABLE:
		handle_routing_table(message);
		break;
	case UPDATE:
		handle_update(message);
		break;
	case CRASH:
		handle_crash(message);
		break;
	case SENDFILE:
		handle_sendfile(message);
		break;
	case SENDFILE_STATS:
		handle_sendfile_stats(message);
		break;
	case LAST_DATA_PACKET:
		handle_last_data_packet(message);
		break;
	case PENULTIMATE_DATA_PACKET:
		handle_penultimate_data_packet(message);
		break;
	}

}
Beispiel #17
0
int EPLIB_init()
{
    process_env_vars();

    init_sig_handlers();

    PMPI_Comm_rank(MPI_COMM_WORLD, &taskid);
    PMPI_Comm_size(MPI_COMM_WORLD, &num_tasks);

    set_local_uuid(taskid);

    allocator_init();

    if (max_ep == 0) return MPI_SUCCESS;

    /* Initialize client */
    client_init(taskid, num_tasks);

    /* Register MPI type and MPI Op before any other cqueue commands */
    cqueue_mpi_type_register();
    cqueue_mpi_op_register();

    /* Initialize communicator handles table */
    handle_init();

    /* Initialize window object table */
    window_init();

    /* Create server world and peer comm for MPI_COMM_WORLD */
    EPLIB_split_comm(MPI_COMM_WORLD, 0, taskid, MPI_COMM_WORLD);

    if (std_mpi_mode == STD_MPI_MODE_IMPLICIT)
        block_coll_request = malloc(max_ep*sizeof(MPI_Request));

    return MPI_SUCCESS;
}
Beispiel #18
0
void restart_child_process()
{
	close(pipe_handles[1]);
	do_del_conn(pipe_handles[2], 2);
	shmq_destroy ();

	shmq_create();
	pipe_create ();

	pid_t pid;

	if ((pid = fork ()) < 0)
		ERROR_LOG("fork child process, fail");
	//parent process
	else if (pid > 0) {
		close (pipe_handles[3]);
		close (pipe_handles[0]);
		do_add_conn (pipe_handles[2], PIPE_TYPE_FD, NULL);

	//child process
	} else {
		close (pipe_handles[1]);
		close (pipe_handles[2]);

		net_init (MAXFDS, pipe_handles[0]);

		if (handle_init () != 0)
			goto out;

		while (!stop || !handle_fini ())
			net_loop (1000, RCVBUFSZ, 0);

out:
		shmq_destroy ();
	}
}
Beispiel #19
0
int main()
{
	int ret = 0,i;

	memset(&modinf,0,sizeof(struct ModuleInfo));

	ret = handle_init(NULL, main_callback, &modinf);
	
	if(ret <= -1)
	{
		printf("初始化失败,程序退出\n");
		return -1;
	}
	
	while(1)
	{
        i=menu();
		printf("%d\n",i);
		//执行用户选择
		switch(i)
		{
		case 0:
		    get_tasknum();
		    break;
		case 1:
		    get_task();
		    break;
		case 2:
            get_taskparanum();
			break;
		case 3:
		    get_taskpara();
		    break;
		case 4:
		    get_taskattunum();
		    break;
		case 5:
		    get_taskattinfo();
		    break;
		case 6:
		    modinf.control(0, HANDLE_STOP_ATASK, NULL, NULL);
		    break;
		case 7:
		    reset_alltask();
		    break;
		case 8:
		    add_task();
		    break;
		case 9:
		    {
             int num;
             printf("请输入任务编号\n");
             scanf("%d",&num);
             getchar();
		     modinf.control(0, HANDLE_DEL_TASK, &num, NULL);
		    }
		    break;
		case 10:
		    {
                int num=1;
                modinf.control(0, HANDLE_START_ALLTASK, &num, NULL);
            }
		    break;
		default:
			printf("ERROR!\n");
		}
		
	}	
	
	//程序退出处理
	modinf.close( );

    return 0;
}
Beispiel #20
0
int main() {
  handle_init();
  tick_timer_service_subscribe(SECOND_UNIT, handle_tick);
  app_event_loop();
  handle_deinit();
}
static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
        const struct fuse_in_header *hdr, const void *data, size_t data_len)
{
    switch (hdr->opcode) {
    case FUSE_LOOKUP: { /* bytez[] -> entry_out */
        const char* name = data;
        return handle_lookup(fuse, handler, hdr, name);
    }

    case FUSE_FORGET: {
        const struct fuse_forget_in *req = data;
        return handle_forget(fuse, handler, hdr, req);
    }

    case FUSE_GETATTR: { /* getattr_in -> attr_out */
        const struct fuse_getattr_in *req = data;
        return handle_getattr(fuse, handler, hdr, req);
    }

    case FUSE_SETATTR: { /* setattr_in -> attr_out */
        const struct fuse_setattr_in *req = data;
        return handle_setattr(fuse, handler, hdr, req);
    }

//    case FUSE_READLINK:
//    case FUSE_SYMLINK:
    case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
        const struct fuse_mknod_in *req = data;
        const char *name = ((const char*) data) + sizeof(*req);
        return handle_mknod(fuse, handler, hdr, req, name);
    }

    case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
        const struct fuse_mkdir_in *req = data;
        const char *name = ((const char*) data) + sizeof(*req);
        return handle_mkdir(fuse, handler, hdr, req, name);
    }

    case FUSE_UNLINK: { /* bytez[] -> */
        const char* name = data;
        return handle_unlink(fuse, handler, hdr, name);
    }

    case FUSE_RMDIR: { /* bytez[] -> */
        const char* name = data;
        return handle_rmdir(fuse, handler, hdr, name);
    }

    case FUSE_RENAME: { /* rename_in, oldname, newname ->  */
        const struct fuse_rename_in *req = data;
        const char *old_name = ((const char*) data) + sizeof(*req);
        const char *new_name = old_name + strlen(old_name) + 1;
        return handle_rename(fuse, handler, hdr, req, old_name, new_name);
    }

//    case FUSE_LINK:
    case FUSE_OPEN: { /* open_in -> open_out */
        const struct fuse_open_in *req = data;
        return handle_open(fuse, handler, hdr, req);
    }

    case FUSE_READ: { /* read_in -> byte[] */
        const struct fuse_read_in *req = data;
        return handle_read(fuse, handler, hdr, req);
    }

    case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
        const struct fuse_write_in *req = data;
        const void* buffer = (const __u8*)data + sizeof(*req);
        return handle_write(fuse, handler, hdr, req, buffer);
    }

    case FUSE_STATFS: { /* getattr_in -> attr_out */
        return handle_statfs(fuse, handler, hdr);
    }

    case FUSE_RELEASE: { /* release_in -> */
        const struct fuse_release_in *req = data;
        return handle_release(fuse, handler, hdr, req);
    }

    case FUSE_FSYNC: {
        const struct fuse_fsync_in *req = data;
        return handle_fsync(fuse, handler, hdr, req);
    }

//    case FUSE_SETXATTR:
//    case FUSE_GETXATTR:
//    case FUSE_LISTXATTR:
//    case FUSE_REMOVEXATTR:
    case FUSE_FLUSH: {
        return handle_flush(fuse, handler, hdr);
    }

    case FUSE_OPENDIR: { /* open_in -> open_out */
        const struct fuse_open_in *req = data;
        return handle_opendir(fuse, handler, hdr, req);
    }

    case FUSE_READDIR: {
        const struct fuse_read_in *req = data;
        return handle_readdir(fuse, handler, hdr, req);
    }

    case FUSE_RELEASEDIR: { /* release_in -> */
        const struct fuse_release_in *req = data;
        return handle_releasedir(fuse, handler, hdr, req);
    }

//    case FUSE_FSYNCDIR:
    case FUSE_INIT: { /* init_in -> init_out */
        const struct fuse_init_in *req = data;
        return handle_init(fuse, handler, hdr, req);
    }

    default: {
        TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
                handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
        return -ENOSYS;
    }
    }
}
Beispiel #22
0
static void main_window_load(Window *window) {
  s_menu_icon_image = gbitmap_create_with_resource(RESOURCE_ID_IMAGE_MENU_ICON_1);

  int num_a_items = 0;

  s_first_menu_items[num_a_items++] = (SimpleMenuItem) {
    .title = "Dairy",
    .callback = menu_select_callback,
};
  s_first_menu_items[num_a_items++] = (SimpleMenuItem) {
    .title = "Fruits and Veggies",
    .callback = menu_select_callback,
  };
  s_first_menu_items[num_a_items++] = (SimpleMenuItem) {
    .title = "Meats and Fish",
    .callback = menu_select_callback,
  };
   s_first_menu_items[num_a_items++] = (SimpleMenuItem) {
    .title = "Grains",
    .callback = menu_select_callback,
  };
 s_first_menu_items[num_a_items++] = (SimpleMenuItem) {
    .title = "Misc.",
    .callback = menu_select_callback,
  };

  s_menu_sections[0] = (SimpleMenuSection) {
    .num_items = NUM_FIRST_MENU_ITEMS,
    .items = s_first_menu_items,
  };
  
  Layer *window_layer = window_get_root_layer(window);
  GRect bounds = layer_get_frame(window_layer);

  s_simple_menu_layer = simple_menu_layer_create(bounds, window, s_menu_sections, NUM_MENU_SECTIONS, NULL);

  layer_add_child(window_layer, simple_menu_layer_get_layer(s_simple_menu_layer));
}

void main_window_unload(Window *window) {
  simple_menu_layer_destroy(s_simple_menu_layer);
  gbitmap_destroy(s_menu_icon_image);
}

static void init() {
  s_main_window = window_create();
  window_set_window_handlers(s_main_window, (WindowHandlers) {
    .load = main_window_load,
    .unload = main_window_unload,
  });

}

static void deinit() {
  window_destroy(s_main_window);
}

int main(void) {
  init();
  products_init();
  handle_init();
  app_event_loop();
  deinit();
  products_deinit();
  handle_deinit();
}
int
main(int ac, char **av)
{
	fd_set *rset, *wset;
	int in, out, max;
	ssize_t len, olen, set_size;

	/* XXX should use getopt */

	__progname = get_progname(av[0]);
	handle_init();

#ifdef DEBUG_SFTP_SERVER
	log_init("sftp-server", SYSLOG_LEVEL_DEBUG1, SYSLOG_FACILITY_AUTH, 0);
#endif

	in = dup(STDIN_FILENO);
	out = dup(STDOUT_FILENO);

#ifdef HAVE_CYGWIN
	setmode(in, O_BINARY);
	setmode(out, O_BINARY);
#endif

	max = 0;
	if (in > max)
		max = in;
	if (out > max)
		max = out;

	buffer_init(&iqueue);
	buffer_init(&oqueue);

	set_size = howmany(max + 1, NFDBITS) * sizeof(fd_mask);
	rset = (fd_set *)xmalloc(set_size);
	wset = (fd_set *)xmalloc(set_size);

	for (;;) {
		memset(rset, 0, set_size);
		memset(wset, 0, set_size);

		FD_SET(in, rset);
		olen = buffer_len(&oqueue);
		if (olen > 0)
			FD_SET(out, wset);

		if (select(max+1, rset, wset, NULL, NULL) < 0) {
			if (errno == EINTR)
				continue;
			exit(2);
		}

		/* copy stdin to iqueue */
		if (FD_ISSET(in, rset)) {
			char buf[4*4096];
			len = read(in, buf, sizeof buf);
			if (len == 0) {
				debug("read eof");
				exit(0);
			} else if (len < 0) {
				error("read error");
				exit(1);
			} else {
				buffer_append(&iqueue, buf, len);
			}
		}
		/* send oqueue to stdout */
		if (FD_ISSET(out, wset)) {
			len = write(out, buffer_ptr(&oqueue), olen);
			if (len < 0) {
				error("write error");
				exit(1);
			} else {
				buffer_consume(&oqueue, len);
			}
		}
		/* process requests from client */
		process();
	}
}
Beispiel #24
0
int main(void) {
  loadDogNames();
  handle_init();
  app_event_loop();
  handle_deinit();
}
int main(int argc, char *argv[])
{
    int sockfd, portno, n;
    struct sockaddr_in serv_addr;
    struct hostent *server;

    char buffer[256];
    if (argc < 3) {
       fprintf(stderr,"usage %s hostname port\n", argv[0]);
       exit(0);
    }
    if (handle_init() == -1)
    {
        error("failed to init GPIO\n");
        exit(0);
    }
    while(1)
    {
        portno = atoi(argv[2]);
        sockfd = socket(AF_INET, SOCK_STREAM, 0);
        if (sockfd < 0) 
        {
            printf("ERROR opening socket\n");
            sleep(1);
            continue;
        }
        server = gethostbyname(argv[1]);
        if (server == NULL) 
        {
            printf("ERROR, no such host\n");
            close(sockfd);
            sleep(1);
            continue;
        }
        bzero((char *) &serv_addr, sizeof(serv_addr));
        serv_addr.sin_family = AF_INET;
        bcopy((char *)server->h_addr, 
             (char *)&serv_addr.sin_addr.s_addr,
             server->h_length);
        serv_addr.sin_port = htons(portno);
        if (connect(sockfd,(struct sockaddr *) &serv_addr,sizeof(serv_addr)) < 0)
        {
            printf("ERROR connecting\n");
            close(sockfd);
            sleep(2);
            continue;
        }
        n = write(sockfd, "device", 6);
        if (n <= 0)
        {
            printf("error transmiting\n");
            close(sockfd);
            sleep(1);
            continue;
        }
        n = read(sockfd, buffer, 255);
        if (n <= 0)
        {
            printf("error transmiting\n");
            close(sockfd);
            sleep(1);
            continue;
        }
        buffer[n] = '\0';
        n = write(sockfd, "received", 8);
        if (n <= 0)
        {
            printf("error transmiting\n");
            close(sockfd);
            sleep(1);
            continue;
        }
        close(sockfd);
        if (handle(buffer) == 0)
            break;
    }
    return 0;
}
Beispiel #26
0
int main ( int argc, char **argv )
{
    char *dev_serial = getenv("BC_DEV");
    if(dev_serial == NULL) {
        dev_serial = "/dev/ttyACM0";
    }

    fd = open ( dev_serial, O_RDWR | O_NOCTTY );
    if ( fd < 0 )
    {
        // TODO: THROW error
        fprintf(stderr, "Failed to open device: %s\n", dev_serial);
        return EXIT_FAILURE;
    }
    // save status port settings.
    tcgetattr ( fd, &oldtio );

    // Setup the serial port.
    struct termios newtio = { 0, };
    newtio.c_cflag     = baudrate | CS8 | CREAD | PARODD;
    newtio.c_iflag     = 0;
    newtio.c_oflag     = 0;
    newtio.c_lflag     = 0;       //ICANON;
    newtio.c_cc[VMIN]  = 1;
    newtio.c_cc[VTIME] = 0;
    tcflush ( fd, TCIFLUSH | TCIOFLUSH );
    tcsetattr ( fd, TCSANOW, &newtio );

    if( argc == 1) {
        printf_information();

    } else if (argc > 1 ) {
        const char *command = argv[1]; 
        // Alarm 
        if(strcasecmp(command, "alarm")  == 0 ) {
           handle_alarm( argc-2, &argv[2]); 
        // Initialize
        } else if (strcasecmp(command, "init") == 0) {
            handle_init();
        }
        else if(strcasecmp(command, "temperature")  == 0 ) {
            handle_temperature();
        }
        else if(strcasecmp(command, "drift")  == 0 ) {
            handle_drift();
        }
        else if ( strcasecmp(command, "brightness") == 0) {
            handle_brightness(argc-2, &argv[2]);
        } else if ( strcasecmp(command, "test") == 0){
            handle_test();
        }

    }






    sync(fd);
   // tcsetattr ( fd, TCSANOW, &oldtio );
    close(fd);
    return EXIT_SUCCESS;
}
int run_fuse_sideload(struct provider_vtab* vtab, void* cookie,
                      uint64_t file_size, uint32_t block_size)
{
    int result;
    uint64_t mem = 0;
    uint64_t avail = 0;

    // If something's already mounted on our mountpoint, try to remove
    // it.  (Mostly in case of a previous abnormal exit.)
    umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_FORCE);

    if (block_size < 1024) {
        fprintf(stderr, "block size (%u) is too small\n", block_size);
        return -1;
    }
    if (block_size > (1<<22)) {   // 4 MiB
        fprintf(stderr, "block size (%u) is too large\n", block_size);
        return -1;
    }

    struct fuse_data fd;
    memset(&fd, 0, sizeof(fd));
    fd.vtab = vtab;
    fd.cookie = cookie;
    fd.file_size = file_size;
    fd.block_size = block_size;
    fd.file_blocks = (file_size == 0) ? 0 : (((file_size-1) / block_size) + 1);

    if (fd.file_blocks > (1<<18)) {
        fprintf(stderr, "file has too many blocks (%u)\n", fd.file_blocks);
        result = -1;
        goto done;
    }

    fd.hashes = (uint8_t*)calloc(fd.file_blocks, SHA256_DIGEST_SIZE);
    if (fd.hashes == NULL) {
        fprintf(stderr, "failed to allocate %d bites for hashes\n",
                fd.file_blocks * SHA256_DIGEST_SIZE);
        result = -1;
        goto done;
    }

    fd.uid = getuid();
    fd.gid = getgid();

    fd.curr_block = -1;
    fd.block_data = (uint8_t*)malloc(block_size);
    if (fd.block_data == NULL) {
        fprintf(stderr, "failed to allocate %d bites for block_data\n", block_size);
        result = -1;
        goto done;
    }
    fd.extra_block = (uint8_t*)malloc(block_size);
    if (fd.extra_block == NULL) {
        fprintf(stderr, "failed to allocate %d bites for extra_block\n", block_size);
        result = -1;
        goto done;
    }

    fd.block_cache_max_size = 0;
    fd.block_cache_size = 0;
    fd.block_cache = NULL;
    mem = free_memory();
    avail = mem - (INSTALL_REQUIRED_MEMORY + fd.file_blocks * sizeof(uint8_t*));
    if (mem > avail) {
        uint32_t max_size = avail / fd.block_size;
        if (max_size > fd.file_blocks) {
            max_size = fd.file_blocks;
        }
        // The cache must be at least 1% of the file size or two blocks,
        // whichever is larger.
        if (max_size >= fd.file_blocks/100 && max_size >= 2) {
            fd.block_cache_max_size = max_size;
            fd.block_cache = (uint8_t**)calloc(fd.file_blocks, sizeof(uint8_t*));
        }
    }

    signal(SIGTERM, sig_term);

    fd.ffd = open("/dev/fuse", O_RDWR);
    if (fd.ffd < 0) {
        perror("open /dev/fuse");
        result = -1;
        goto done;
    }

    char opts[256];
    snprintf(opts, sizeof(opts),
             ("fd=%d,user_id=%d,group_id=%d,max_read=%u,"
              "allow_other,rootmode=040000"),
             fd.ffd, fd.uid, fd.gid, block_size);

    result = mount("/dev/fuse", FUSE_SIDELOAD_HOST_MOUNTPOINT,
                   "fuse", MS_NOSUID | MS_NODEV | MS_RDONLY | MS_NOEXEC, opts);
    if (result < 0) {
        perror("mount");
        goto done;
    }
    uint8_t request_buffer[sizeof(struct fuse_in_header) + PATH_MAX*8];
    while (!terminated) {
        fd_set fds;
        struct timeval tv;
        FD_ZERO(&fds);
        FD_SET(fd.ffd, &fds);
        tv.tv_sec = 1;
        tv.tv_usec = 0;
        int rc = select(fd.ffd+1, &fds, NULL, NULL, &tv);
        if (rc <= 0) {
            continue;
        }
        ssize_t len = TEMP_FAILURE_RETRY(read(fd.ffd, request_buffer, sizeof(request_buffer)));
        if (len < 0) {
            if (errno != EINTR) {
                perror("read request");
                if (errno == ENODEV) {
                    result = -1;
                    break;
                }
            }
            continue;
        }

        if ((size_t)len < sizeof(struct fuse_in_header)) {
            fprintf(stderr, "request too short: len=%zu\n", (size_t)len);
            continue;
        }

        struct fuse_in_header* hdr = (struct fuse_in_header*) request_buffer;
        void* data = request_buffer + sizeof(struct fuse_in_header);

        result = -ENOSYS;

        switch (hdr->opcode) {
             case FUSE_INIT:
                result = handle_init(data, &fd, hdr);
                break;

             case FUSE_LOOKUP:
                result = handle_lookup(data, &fd, hdr);
                break;

            case FUSE_GETATTR:
                result = handle_getattr(data, &fd, hdr);
                break;

            case FUSE_OPEN:
                result = handle_open(data, &fd, hdr);
                break;

            case FUSE_READ:
                result = handle_read(data, &fd, hdr);
                break;

            case FUSE_FLUSH:
                result = handle_flush(data, &fd, hdr);
                break;

            case FUSE_RELEASE:
                result = handle_release(data, &fd, hdr);
                break;

            default:
                fprintf(stderr, "unknown fuse request opcode %d\n", hdr->opcode);
                break;
        }

        if (result != NO_STATUS) {
            struct fuse_out_header outhdr;
            outhdr.len = sizeof(outhdr);
            outhdr.error = result;
            outhdr.unique = hdr->unique;
            TEMP_FAILURE_RETRY(write(fd.ffd, &outhdr, sizeof(outhdr)));
        }
    }

  done:
    fd.vtab->close(fd.cookie);

    result = umount2(FUSE_SIDELOAD_HOST_MOUNTPOINT, MNT_DETACH);
    if (result < 0) {
        printf("fuse_sideload umount failed: %s\n", strerror(errno));
    }

    if (fd.ffd) close(fd.ffd);
    if (fd.block_cache) {
        uint32_t n;
        for (n = 0; n < fd.file_blocks; ++n) {
            free(fd.block_cache[n]);
        }
        free(fd.block_cache);
    }
    free(fd.hashes);
    free(fd.block_data);
    free(fd.extra_block);

    return result;
}
static int pseudo_device_ioctl(struct inode *inode, struct file *file,
                               unsigned int cmd, unsigned long arg)
{

    switch (cmd)
    {

    case RD_INIT:
        down_interruptible(&mutex);
        handle_init();
        up(&mutex);
        break;
    case RD_CREAT:
        down_interruptible(&mutex);
        handle_creat_call(arg);
        up(&mutex);
        break;

    case RD_MKDIR:
        down_interruptible(&mutex);
        handle_mkdir_call(arg);
        up(&mutex);
        break;

    case RD_OPEN:
        down_interruptible(&mutex);
        handle_open_call(arg);
        up(&mutex);
        break;

    case RD_CLOSE:
        down_interruptible(&mutex);
        handle_close_call(arg);
        up(&mutex);
        break;

    case RD_READ:
        down_interruptible(&mutex);
        handle_read_call(arg);
        up(&mutex);
        break;

    case RD_WRITE:
        down_interruptible(&mutex);
        handle_write_call(arg);
        up(&mutex);
        break;

    case RD_SEEK:
        down_interruptible(&mutex);
        handle_seek_call(arg);
        up(&mutex);
        break;

    case RD_READDIR:
        down_interruptible(&mutex);
        handle_readdir_call(arg);
        up(&mutex);
        break;

    case RD_UNLINK:
        down_interruptible(&mutex);
        handle_unlink_call(arg);
        up(&mutex);
        break;

    case RD_TEST:
        down_interruptible(&mutex);
        handle_test(arg);
        up(&mutex);        

    default:
        return -EINVAL;
        break;
    }

    return 0;
}
Beispiel #29
0
void process_messages()
{
#ifdef UART0_DISCONNECT_ON_DTR
	if ((PORT_UART0_DTR & (1 << PIN_UART0_DTR)))
	{
		if (state == StateConnected)
		{
			send_error(ErrorDisconnect, 0);
		}
		uart0_flush();
		return;
	}
#endif
	MessageParseResult res;
	while ((res = message_parse()) == ParseOk)
	{
		switch (message.identifier)
		{
			case IdAckMessage:
				handle_ack();
				break;
			case IdInitMessage:
				handle_init();
				break;
			case IdFootSwitchMessage:
				handle_footswitch();
				break;
			case IdInterlockResetMessage:
				handle_int_reset();
				break;
			case IdServoCtrlMessage:
				handle_servo_ctrl();
				break;
			case IdDockingLimitMessage:
				handle_docking_limit();
				break;
			case IdErrorMessage:
				handle_error();
				break;
			case IdDockingTareMessage:
				handle_docking_tare();
				break;
			case IdSettingsMessage:
				handle_settings();
				break;
			default:
				send_error(ErrorUnhandled, message.sequence);
				return;
		}
	}

	switch (res)
	{
		case ParseNotEnoughData: // buffer empty, done
			break;
//		case ParseInvalidIdentifier: // fall
//		case ParseInvalidSizeError: // fall
//		case ParseChecksumError: // fall
		default:
			send_error(ErrorParser, 0);
			break;
	}
}
Beispiel #30
0
int main() {
  handle_init();
  app_event_loop();
  handle_deinit();
  return 0;
}