android
2024-07-25 2dcf9cc893a27c50d538baf8b5d47ff06fe495d8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <jni.h>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <android/log.h>
 
#define TAG "BuzzerApp"
#define LOGI(fmt, args...) __android_log_print(ANDROID_LOG_INFO, TAG, fmt, ##args)
#define LOGD(fmt, args...) __android_log_print(ANDROID_LOG_DEBUG, TAG, fmt, ##args)
#define LOGE(fmt, args...) __android_log_print(ANDROID_LOG_ERROR, TAG, fmt, ##args)
 
static char pwm_path[100];
 
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_serial_BuzzerControl_pwmOpen(JNIEnv *env, jclass clazz, jstring id) {
    // TODO: implement pwmOpen()
    char *id_native;
    char  temp[100];
    int   fd;
    id_native = (char *)env->GetStringUTFChars(id, 0);
    memset(pwm_path, 0, sizeof (pwm_path));
    snprintf(pwm_path, sizeof (pwm_path), "/sys/class/pwm/pwmchip%s/pwm0", id_native);
 
    memset(temp, 0, sizeof (temp));
    if(access(pwm_path, F_OK))
    {
        LOGE("pwm_path:%s, access=%d", pwm_path, access(pwm_path, F_OK));
        snprintf(temp, sizeof (temp), "/sys/class/pwm/pwmchip%s/export", id_native);
        fd = open(temp, O_WRONLY);
        if(fd < 0)
        {
            if( strstr(strerror(errno), "Permission denied") != nullptr )
            {
                LOGE("open %s error:%s\n", temp, strerror(errno));
                return 1;
            }
            LOGE("open %s error:%s\n", temp, strerror(errno));
            return -1;
        }
 
        if(1 != write(fd, "0", 1))
        {
            LOGE("Write '0' to pwmchip%s/export error\n", id_native);
            close(fd);
            return -2;
        }
        close(fd);
    }
    env->ReleaseStringUTFChars(id, id_native);
    return 0;
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_example_serial_BuzzerControl_pwmConfig(JNIEnv *env, jclass clazz, jstring attr,
                                                jstring val) {
    // TODO: implement pwmConfig()
    char file_path[100];
    char *attr_native;
    char *val_native;
    int  len;
    int  fd = -1;
    int  ret;
 
    if(attr == NULL || val == NULL)
    {
        LOGE("argument invalid\n");
        return -1;
    }
 
    attr_native = (char *)env->GetStringUTFChars(attr, 0);
    val_native = (char *)env->GetStringUTFChars(val, 0);
 
    memset(file_path, 0, sizeof(file_path));
    snprintf(file_path, sizeof (file_path), "%s/%s", pwm_path, attr_native);
    fd = open(file_path, O_WRONLY);
    if (fd < 0)
    {
        if( strstr(strerror(errno), "Permission denied") != nullptr )
        {
            LOGE("open %s error:%s\n", file_path, strerror(errno));
            fd=1;
        } // 表示缺少权限
        LOGE("[%s] open %s error:%s\n", __FUNCTION__ , file_path, strerror(errno));
        return fd;
    }
 
    len = strlen(val_native);
    if(len != write(fd, val_native, len))
    {
        if ( strstr(strerror(errno), "Invalid") != nullptr)
        {
            close(fd);
            return 2; //表示输入的参数无效
        }
        LOGE("[%s] write %s to %s error: %s\n", __FUNCTION__ , val_native, file_path, strerror(errno));
        close(fd);
        return -2;
    }
 
    close(fd);
    env->ReleaseStringUTFChars(attr,attr_native);
    env->ReleaseStringUTFChars(val, val_native);
    return 0;
}