/****************************************************************************** * * Copyright (C) 2009-2018 Realtek Corporation. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ******************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #define UNIX_DOMAIN "@/data/misc/bluedroid/rtkbt_service.sock" typedef struct Rtk_Socket_Data { unsigned char type; //hci,other,inner unsigned char opcodel; unsigned char opcodeh; unsigned char parameter_len; unsigned char parameter[0]; }Rtk_Socket_Data; /*typedef struct { unsigned short event; unsigned short len; unsigned short offset; unsigned short layer_specific; unsigned char data[]; } HC_BT_HDR; */ const char shortOptions[] = "f:r:h"; const struct option longOptions[] = { {"fullhcicmd", required_argument, NULL, 'f'}, {"read", required_argument, NULL, 'r'}, {"help", no_argument, NULL, 'h'}, {0, 0, 0, 0} }; static void usage(void) { fprintf(stderr, "Usage: rtkcmd [options]\n\n" "Options:\n" "-f | --fullhcicmd [opcode,parameter_len,parameter] send hci cmd\n" "-r | --read [address] read register address \n" "-h | --help Print this message\n\n"); } int Rtkbt_Sendcmd(int socketfd,char *p) { char *token = NULL; int i=0; unsigned short OpCode = 0; unsigned char ParamLen = 0; unsigned char ParamLen_1 = 0; int sendlen = 0; int params_count=0; int ret=0; Rtk_Socket_Data *p_buf = NULL; token = strtok(p,","); if (token != NULL) { OpCode = strtol(token, NULL, 0); //printf("OpCode = %x\n",OpCode); params_count++; } else { //ret = FUNCTION_PARAMETER_ERROR; printf("parameter error\n"); return -1; } token = strtok(NULL, ","); if (token != NULL) { ParamLen = strtol(token, NULL, 0); //printf("ParamLen = %d\n",ParamLen); params_count++; } else { printf("parameter error\n"); return -1; } p_buf=(Rtk_Socket_Data *)malloc(sizeof(Rtk_Socket_Data) + sizeof(char)*ParamLen); p_buf->type = 0x01; p_buf->opcodeh = OpCode>>8; p_buf->opcodel = OpCode&0xff; p_buf->parameter_len = ParamLen; ParamLen_1 = ParamLen; while (ParamLen_1--) { token = strtok(NULL, ","); if (token != NULL) { p_buf->parameter[i++] = strtol(token, NULL, 0); params_count++; } else { printf("parameter error\n"); return -1; } } if (params_count != ParamLen + 2) { printf("parameter error\n"); return -1; } sendlen=sizeof(Rtk_Socket_Data)+sizeof(char)*p_buf->parameter_len; ret=write(socketfd,p_buf,sendlen); if(ret!=sendlen) return -1; free(p_buf); return 0; } int Rtkbt_Getevent(int sock_fd) { unsigned char type = 0; unsigned char event = 0; unsigned char len = 0; unsigned char *recvbuf = NULL; unsigned char tot_len = 0; int ret=0; int i; ret = read(sock_fd, &type, 1); if(ret <= 0) return -1; ret = read(sock_fd, &event, 1); if(ret <= 0) return -1; ret = read(sock_fd, &len, 1); if(ret <= 0) return -1; tot_len = len + 2; recvbuf=(unsigned char *)malloc(sizeof(char)*tot_len); recvbuf[0] = event; recvbuf[1] = len; ret = read(sock_fd,recvbuf+2,len); if(ret < len) return -1; printf("Event: "); for(i=0;i=0) { if(Rtkbt_Getevent(socketfd)<0) printf("Getevent fail\n"); } break; } case 'r': { printf("read register %s\n",optarg); //Rtkbt_Readreg(socketfd,optarg); break; } case 'h': { usage(); break; } } } close(socketfd); }