Android源码编译系统中添加C/C++项目cantest

rk3288 编译应用程序 https://www.cnblogs.com/linux-37ge/p/11291518.html
在编译Android系统时,可以增加一些C或者C++的工具程序,从而可以在目标系统的shell命令下使用该工具。
应该建在external目录下

1. 工程文件

在Android系统的frameworks下建立cantest文件夹,并创建Android.mk cantest.c

roc@roc-System-Product-Name:/home/workspace/sdk-7.1/frameworks$ mkdir cantest
roc@roc-System-Product-Name:/home/workspace/sdk-7.1/frameworks$ cd cantest/
roc@roc-System-Product-Name:/home/workspace/sdk-7.1/frameworks/cantest$ touch cantest.c Android.mk
roc@roc-System-Product-Name:/home/workspace/sdk-7.1/frameworks/cantest$ ls
Android.mk  cantest.c

编写c 文件

 /*
 *  can_test_send_and_dump.c
 *
 *  Created on: 2018-8-25
 *  Author: tanghanyue
*/
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/can.h>
#include <linux/can/raw.h>
 
int main(int argc, char *argv[])
{
	int s, nbytes;
        unsigned char number=0;
	struct sockaddr_can addr;
	struct ifreq ifr;
	struct can_frame frame;
        struct can_frame frame_send;
	struct can_filter rfilter[1];
        pid_t pid = -1;
	int i;
	/* handle (optional) flags first */
	if(argc != 2) {
	    fprintf(stderr, "Usage:  %s <-r> <can interface name> for receiving\nor <-s> <can interface name> for sending\n", argv[0]);
	    exit(1);
	}
	/* create socket */
	if ((s = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0)
	{
	   perror("Create socket failed");
	   exit(-1);
	}
 
	/* set up can interface */
	strcpy(ifr.ifr_name, argv[1]);
	printf("can port is %s\n",ifr.ifr_name);
	/* assign can device */
	ioctl(s, SIOCGIFINDEX, &ifr);
	    addr.can_family = AF_CAN;
	addr.can_ifindex = ifr.ifr_ifindex;
	    /* bind can device */
	if(bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
	{
	     perror("Bind can device failed\n");
	     close(s);
	     exit(-2);
	}
 
        pid = fork();
        if(pid == -1) 
        { 
             perror("\n\tfailed to fork!!\n\n");
             return  -1;
        }
        else if(pid==0)/* configure receiving can data*/
	{
	   /* set filter for only receiving packet with can id 0x88 */
	   rfilter[0].can_id = 0x88;
	   rfilter[0].can_mask = CAN_SFF_MASK;
	   if(setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, &rfilter, sizeof(rfilter)) < 0)
	   {
	        perror("set receiving filter error\n");
	        close(s);
	        exit(-3);
	   }
	   /* keep reading */
	   while(1){
 
	       nbytes = read(s, &frame, sizeof(frame));
	       if(nbytes > 0)
	       {
		   printf("read datas:%s ID=%#x data length=%d\n", ifr.ifr_name, frame.can_id, frame.can_dlc);
		   for ( i=0; i < frame.can_dlc; i++)
		        printf("%#x ", frame.data[i]);
		   printf("\n");
	       }
	       printf("read can data over\n");
 
	   }
	}/* configure sending can data*/
        else
	{   
              while(1)
              {
		/* configure can_id and can data length */
	        frame_send.can_id = 0x88;
		frame_send.can_dlc = 8;
		printf("%s ID=%#x data length=%d\n", ifr.ifr_name, frame_send.can_id, frame_send.can_dlc);
		/* prepare data for sending: 0x11,0x22...0x88 */
		for (i=0; i<8; i++)
		{
		    frame_send.data[i] = ((i+1)<<4) | (i+1);
                    frame_send.data[7] =number;
		    printf("%#x ", frame_send.data[i]);
		}
		printf("success to Sent out\n");
		/* Sending data */
		if(write(s, &frame_send, sizeof(frame_send)) < 0)
	        {
		    perror("Send failed");
		    close(s);
		    exit(-4);
	        }
                sleep(1);
                number++;
             }
            
	 }
	close(s);
	return 0;
}

编写Android.mk

LOCAL_PATH:= $(call my-dir)

include $(CLEAR_VARS)
LOCAL_SRC_FILES:= cantest.c
LOCAL_MODULE:= cantest
LOCAL_32_BIT_ONLY := true

include $(BUILD_EXECUTABLE)

编译
进入Android系统源代码根目录,先进行Android系统环境配置,然后执行以下命令来编译这个C项目:

source build/envsetup.sh
mmm frameworks/testing/cantest

展开本句:??? 或者进入目录 mm

生成可执行文件

[100% 8/8] Install: out/target/product/rk3288/system/bin/cantest

push 文件到终端 cmd adb

C:\adb>adb root

C:\adb>adb remount
remount succeeded

C:\adb>adb push cantest /system/bin
cantest: 1 file pushed. 0.5 MB/s (13912 bytes in 0.029s)

C:\adb>

执行

130|rk3288:/system/bin $ ./cantest                                             
Usage:  ./cantest <-r> <can interface name> for receiving
or <-s> <can interface name> for sending
1|rk3288:/system/bin $ 
252|rk3288:/system/bin $ cantest can0                                          
can port is can0
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0 success to Sent out
read can data over
Send failed: Network is down

打开can0

ifconfig: bad argument 'dwon'
1|rk3288:/ # ifconfig can0 down
rk3288:/ # ip link set can0 type can bitrate 500000
rk3288:/ # ifconfig can0 up
ifconfig: ioctl 8914: No such device
1|rk3288:/ # ifconfig can0 up
ifconfig: ioctl 8914: No such device
1|rk3288:/ # ifconfig can0 up
**       serail_process:can0
can port is can0
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x1 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x2 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x3 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x4 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x5 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x6 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x7 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x8 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0x9 success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0xa success to Sent out
can0 ID=0x88 data length=8
0x11 0x22 0x33 0x44 0x55 0x66 0x77 0xb success to Sent out
Send failed: No buffer space available
252|rk3288:/ #
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值