⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyboard_get.c

📁 sparc硬件平台上的键盘驱动
💻 C
字号:
/************************************************************  Copyright (C), 2007,DTK Computer.  FileName: keyboard_get.c  Author:   rome     Version : 1.0.0         Date:2007.03.15  Description:     模拟键盘中断。        Version:         DTK-EMMI-1.0.0  Function List:     History:               <author>  <time>   <version >   <desc>      rome    2007/03/22  1.0.0     build this moudle  ***********************************************************/#include <unistd.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#define LOCALHOST "localhost"#define TEST_HOST 7002#define BUFFER_LEN 1024#ifdef CROSS#include <uip/uip.h>#include <uip/uip_arp.h>#include <uip/uip_greth.h>static void get_key(void){    while(1)    {        if(uip_len > 0)         {            uip_input();            //uip_process(UIP_DATA);            if(uip_len > 0)            {                printf("we get the key---->; %s\n", uip_buf);                uip_len = 0;            }        }    }}int main(void){    u16_t ipaddr[2];    uip_init();    uip_ipaddr(ipaddr, 192,168,0,153);    uip_sethostaddr(ipaddr);    uip_ipaddr(ipaddr, 192,168,0,1);    uip_setdraddr(ipaddr);    uip_ipaddr(ipaddr, 255,255,255,0);    uip_setnetmask(ipaddr);       printf("the address is %d, %d\n", uip_hostaddr[0], uip_hostaddr[1]);    uip_listen(HTONS(TEST_HOST));    while(1)    {        if (uip_connected())        {            printf("has connected\n");            break;        }    }    //uip_connect(ipaddr, HTONS(TEST_HOST));    get_key();    return 0;}#else /* define x86 */#include <sys/socket.h>#include <sys/types.h>#include <arpa/inet.h>#include <netdb.h>#include <stdio.h>#include <unistd.h>#include <key_buffer.h>#include <drv_keyboard.h>#include <mmi_lock.h>static int get_listened_sock(void);static void service_clent_by_sock(int sock);static void simulate_get(void);int main(void) {     int sock;    drv_keyboard_init();    simulate_get();    sock = get_listened_sock();    service_clent_by_sock(sock);    close(sock);    return 0;} static void *simulate_regular_get(void *user_data){    char ch;    while(1)    {        ch = drv_keyboard_get(0);        printf("thread %p get the char: %c\n", mmi_thread_self(), ch);    }}static void *simulate_timeout_get(void *user_data){    int ch;    while(1)    {        ch = drv_keyboard_get(6);        if (GET_OVER_TIME == ch)        {            printf("thread %p get timeout. the timeout key is: %x\n",                     mmi_thread_self(), ch);        }        else        {            printf("thread %p get the char: %c\n", mmi_thread_self(), ch);        }    }}static void simulate_get(void){    mmi_thread_t regular_get;    mmi_thread_t timeout_get;    if (mmi_thread_create(&regular_get, NULL, simulate_regular_get, NULL))    {        printf("simulate_get: create thread for simulate regular get error\n");        exit(1);    }    if (mmi_thread_create(&timeout_get, NULL, simulate_timeout_get, NULL))    {        printf("simulate_get: create thread for timeout get error\n");        exit(1);    }}static void service_clent_by_sock(int sock){    char buf[BUFFER_LEN];     int msgsock;     int rval;     struct sockaddr tcpaddr;     int len = sizeof(struct sockaddr);     do     {         msgsock = accept(sock, (struct sockaddr *)&tcpaddr, (int *)&len);         if (-1 == msgsock)         {            perror("accept");         }                else         {            do            {                 memset(buf, 0, sizeof(buf));                                 if ((rval = recv(msgsock, buf, 1024, 0)) < 0)                 {                    perror("reading stream message");                 }                if (0 == rval)                 {                    printf("ending connection \n");                 }                else                 {                    printf("-->;%s\n", buf);                     int i = 0;                    while(buf[i])                    {                        if (buf[i] != '\n') /* 跳地回车符号 */                        {                            key_buffer_put(key_buffer, buf[i]);                        }                        i++;                    }                }            }while (rval != 0);                     close(msgsock);         }    } while (1); }static int get_listened_sock(){    int sock;     int length;     struct sockaddr_in server;         /* 建立套接字 */     sock = socket(AF_INET, SOCK_STREAM, 0);     if (sock < 0)     {         perror("opening stream socket");         exit(1);     }         bzero(&server,sizeof(struct sockaddr_in));     server.sin_family = AF_INET;     server.sin_port = htons(TEST_HOST);     server.sin_addr.s_addr=htonl(INADDR_ANY);     if (bind(sock, (struct sockaddr *)&server, sizeof(server)) < 0)    {         perror("binding stream socket");         exit(1);     }         /* 找出指定的端口号并打印出来 */     length = sizeof(server);     if (getsockname(sock, (struct sockaddr *)&server, &length) < 0)    {         perror("getting socket name");         exit(1);     }     printf("socket port %d\n", ntohs(server.sin_port));         /* 开始接收连接 */     listen(sock, 5);     return sock;}#endif /* end define x86 */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -