Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
26e9de4
Primeiro commit atividade 3
FerreiraBen May 16, 2024
f089124
compilar e rodar o drive
May 16, 2024
8dad5c2
Adicionando código do smartlamp
ChigorGomes May 17, 2024
5f1b3f1
Melhorias no comando via serial
ChigorGomes May 17, 2024
4cf35ae
GET_LDR inserted in code
May 17, 2024
3c6c724
Task 2.1 done
May 17, 2024
abcd13d
Merge branch 'mateusPreste:main' into task2
ChigorGomes May 17, 2024
9979791
Merge pull request #1 from ChigorGomes/task2
ChigorGomes May 17, 2024
db4e883
merge
ChigorGomes May 17, 2024
2f2273a
Merge pull request #2 from ChigorGomes/cicero
ChigorGomes May 17, 2024
2218789
Merge branch 'mateusPreste:main' into vinicius
ChigorGomes May 17, 2024
1d4e6bb
Merge branch 'main' into vinicius
ChigorGomes May 17, 2024
c8e024b
Merge pull request #3 from ChigorGomes/vinicius
ChigorGomes May 17, 2024
4a27198
Images insertion
May 17, 2024
f1a1773
Alterar o firmware do smartlamp
May 17, 2024
55196ec
Merge pull request #4 from ChigorGomes/enzo
ChigorGomes May 17, 2024
3e30c6e
Merge pull request #5 from ChigorGomes/task4-1
ChigorGomes May 18, 2024
1c9915e
Atividade 3 Hands on Linux Kernel
Copola1 May 18, 2024
e5d14e1
Update README.md
henriqu3nzo May 18, 2024
5288ea1
Update README.md
ChigorGomes May 18, 2024
a96115f
Merge pull request #6 from ChigorGomes/vinicius
ChigorGomes May 18, 2024
5e13f19
Update README.md
ChigorGomes May 18, 2024
0777027
Refatorando task 3
vinicius-pereira99 May 20, 2024
fb3c020
Adicionando funções de leitura para Task 4
vinicius-pereira99 May 20, 2024
376698a
Update smartlamp.c
ChigorGomes May 24, 2024
28c689a
Update README.md
ChigorGomes May 24, 2024
9db6e11
Update README.md
ChigorGomes May 24, 2024
1c21836
Atualizado imagem
ChigorGomes May 24, 2024
59f7e4a
Update README.md
ChigorGomes May 24, 2024
81bd9ee
Update README.md
ChigorGomes May 24, 2024
c8ef3b1
Merge branch 'main' into main
ChigorGomes Jun 10, 2024
b68daa8
mudançasledldr
Thaianenaiara Jun 11, 2024
6e252de
Tarefa4.1 adicionada
Thaianenaiara Jun 11, 2024
3304889
Merge pull request #8 from ChigorGomes/Tarefa1
ChigorGomes Jun 11, 2024
335eae7
melhoria no código smartlamp.ino
ChigorGomes Jun 12, 2024
795a87e
Merge branch 'mateusPreste:main' into main
ChigorGomes Jun 12, 2024
4091f10
Update README.md
ChigorGomes Jun 12, 2024
6d4ddd0
Vinicius (#10)
Copola1 Jun 12, 2024
57ee9e0
Henrique (#11)
henriqu3nzo Jun 13, 2024
52def09
tarefa 2-3 (#9)
natalia-rufino Jun 13, 2024
84d7c75
Adicionando tarefa 1 e 4 (#12)
ChigorGomes Jun 13, 2024
b119fbf
Update README.md
ChigorGomes Jun 13, 2024
1d7ac80
Update README.md
ChigorGomes Jun 13, 2024
e055dda
Atualizando as cartas
ChigorGomes Jun 13, 2024
38ae2b3
Update README.md
ChigorGomes Jun 13, 2024
f74bec2
Adicionado as cartas
ChigorGomes Jun 14, 2024
5964c0d
Update README.md
ChigorGomes Jun 14, 2024
8a3ecb5
Adicionando carta 2
ChigorGomes Jun 14, 2024
1853f70
Update README.md
ChigorGomes Jun 14, 2024
0be4240
Vinicius (#13)
Copola1 Jun 14, 2024
f3f585c
Update README.md
ChigorGomes Jun 14, 2024
67c2309
Update smartlamp.ino
ChigorGomes Jun 14, 2024
db5ab6b
attr_store()
Jun 15, 2024
4b641e4
Vinicius (#14)
Copola1 Jun 15, 2024
c62b5f5
tarefa 3-1-2 (#15)
natalia-rufino Jun 15, 2024
57a8c5e
finalização do repositório
ChigorGomes Jun 17, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@


# DevTITANS 05 - HandsOn Linux - Equipe 0X
# DevTITANS 05 - HandsOn Linux - Equipe 06

Bem-vindo ao repositório da Equipe 0X do HandsON de Linux do DevTITANS! Este projeto contém um firmware para o ESP32 escrito em formato Arduino `.ino`, bem como um driver do kernel Linux escrito em C. O objetivo é demonstrar como criar uma solução completa de hardware e software que integra um dispositivo ESP32 com um sistema Linux.
Bem-vindo ao repositório da Equipe 06 do HandsON de Linux do DevTITANS! Este projeto contém um firmware para o ESP32 escrito em formato Arduino `.ino`, bem como um driver do kernel Linux escrito em C. O objetivo é demonstrar como criar uma solução completa de hardware e software que integra um dispositivo ESP32 com um sistema Linux.

## Tabela de Conteúdos

Expand All @@ -17,18 +17,19 @@ Bem-vindo ao repositório da Equipe 0X do HandsON de Linux do DevTITANS! Este pr

## Contribuidores

<img src="https://github.com/DevTITANS05/Hands-On-Linux-fork-/assets/21023906/85e61f3e-476c-47a4-82d5-4054e856c67b" width="180" >
<img src="https://github.com/DevTITANS05/Hands-On-Linux-fork-/assets/21023906/85e61f3e-476c-47a4-82d5-4054e856c67b" width="180" >
<img src="https://github.com/DevTITANS05/Hands-On-Linux-fork-/assets/21023906/85e61f3e-476c-47a4-82d5-4054e856c67b" width="180" >
<img src="https://github.com/DevTITANS05/Hands-On-Linux-fork-/assets/21023906/85e61f3e-476c-47a4-82d5-4054e856c67b" width="180" >
<img src="https://github.com/DevTITANS05/Hands-On-Linux-fork-/assets/21023906/85e61f3e-476c-47a4-82d5-4054e856c67b" width="180" >
<img src="https://github.com/DevTITANS05/Hands-On-Linux-fork-/assets/21023906/85e61f3e-476c-47a4-82d5-4054e856c67b" width="180" >
<div>
<img src="https://github.com/ChigorGomes/Hands-On-Linux/blob/main/assets/cartao01.png" width="180" >
<img src="https://github.com/ChigorGomes/Hands-On-Linux/blob/main/assets/cartao2.jpg" width="180" >
<img src="https://github.com/ChigorGomes/Hands-On-Linux/blob/main/assets/cartao3.jpg" width="180" >
<img src="https://github.com/ChigorGomes/Hands-On-Linux/blob/main/assets/cartao5.JPG" width="180" >
<img src="https://github.com/ChigorGomes/Hands-On-Linux/blob/main/assets/cartao4.png" width="180" >
</div>

- **Nome do(a) Aluno(a) 01:** Desenvolvedor do Firmware e Mantenedor do Projeto
- **Nome do(a) Aluno(a) 02:** Desenvolvedor do Firmware
- **Nome do(a) Aluno(a) 03:** Desenvolvedor do Driver Linux
- **Nome do(a) Aluno(a) 04:** Desenvolvedor do Driver Linux
- **Nome do(a) Aluno(a) 05:** Desenvolvedor do Firmware e Escritor da Documentação
- **Cícero Higor** Desenvolvedor do Firmware e Mantenedor do Projeto
- **Enzo Henrique** Desenvolvedor do Firmware e Escritor da Documentação
- **Natalia Rufino** Desenvolvedor do Driver Linux
- **Thaiane Siqueira** Desenvolvedor do Firmware
- **Vinicius Ferreira** Desenvolvedor do Driver Linux

## Introdução

Expand All @@ -44,7 +45,7 @@ Este projeto serve como um exemplo para desenvolvedores interessados em construi

- **Driver do Kernel Linux:**
- Rotinas de inicialização e limpeza.
- Operações de arquivo de dispositivo (`GET_LED`, `SET_LED`, `GET_LDR`).
- Operações de arquivo de dispositivo (`GET_LED`, `SET_LED`, `GET_LDR`,`GET_TEMP`,`GET_HUM`).
- Comunicação com o ESP32 via Serial.

## Requisitos
Expand All @@ -54,6 +55,8 @@ Este projeto serve como um exemplo para desenvolvedores interessados em construi
- Máquina Linux
- Protoboard e Cabos Jumper
- Sensor LDR
- LED
- Sensor DHT11

- **Software:**
- Arduino IDE
Expand Down Expand Up @@ -141,4 +144,4 @@ Depois que o driver e o firmware estiverem configurados, você poderá interagir

## Contato

Para perguntas, sugestões ou feedback, entre em contato com o mantenedor do projeto em [maintainer@example.com](mailto:maintainer@example.com).
Para perguntas, sugestões ou feedback, entre em contato com o mantenedor do projeto em [higorgomesousa@gmail.com](higorgomesousa@gmail.com).
Binary file added assets/arduino_serial.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/arduino_serial2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cartao01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cartao2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cartao3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cartao4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cartao5.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/montagem1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/montagem2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added esp32/montagem.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions esp32/pinos.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
1 - Montagem do Protótipo ESP32. Para os pinos usamos:
PINO_LED = 23
PINO_LDR = 33
PINO_DHT = 26

4 changes: 2 additions & 2 deletions smartlamp-kernel-module/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
obj-m += SUBISTITUA_PELO_NOME_DO_MODULO.o
obj-m += probe.o serial.o serial_write.o smartlamp.o #sysfs.o
PWD := $(CURDIR)

all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
1 change: 1 addition & 0 deletions smartlamp-kernel-module/led
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
100
6 changes: 3 additions & 3 deletions smartlamp-kernel-module/probe.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ static uint usb_in, usb_out; // Endereços das portas de e
static char *usb_in_buffer, *usb_out_buffer; // Buffers de entrada e saída da USB
static int usb_max_size; // Tamanho máximo de uma mensagem USB

#define VENDOR_ID SUBSTITUA_PELO_VENDORID /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID SUBSTITUA_PELO_PRODUCTID /* Encontre o ProductID do smartlamp */
#define VENDOR_ID 0x10c4 /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID 0xea60 /* Encontre o ProductID do smartlamp */
static const struct usb_device_id id_table[] = { { USB_DEVICE(VENDOR_ID, PRODUCT_ID) }, {} };

static int usb_probe(struct usb_interface *ifce, const struct usb_device_id *id); // Executado quando o dispositivo é conectado na USB
Expand Down Expand Up @@ -56,4 +56,4 @@ static void usb_disconnect(struct usb_interface *interface) {
printk(KERN_INFO "SmartLamp: Dispositivo desconectado.\n");
kfree(usb_in_buffer); // Desaloca buffers
kfree(usb_out_buffer);
}
}
25 changes: 21 additions & 4 deletions smartlamp-kernel-module/serial.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ static uint usb_in, usb_out; // Endereços das portas de e
static char *usb_in_buffer, *usb_out_buffer; // Buffers de entrada e saída da USB
static int usb_max_size; // Tamanho máximo de uma mensagem USB

#define VENDOR_ID SUBSTITUA_PELO_VENDORID /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID SUBSTITUA_PELO_PRODUCTID /* Encontre o ProductID do smartlamp */
#define VENDOR_ID 0x10c4 /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID 0xea60 /* Encontre o ProductID do smartlamp */
static const struct usb_device_id id_table[] = { { USB_DEVICE(VENDOR_ID, PRODUCT_ID) }, {} };

static int usb_probe(struct usb_interface *ifce, const struct usb_device_id *id); // Executado quando o dispositivo é conectado na USB
Expand Down Expand Up @@ -74,16 +74,33 @@ static int usb_read_serial() {
// Lê os dados da porta serial e armazena em usb_in_buffer
// usb_in_buffer - contem a resposta em string do dispositivo
// actual_size - contem o tamanho da resposta em bytes
strcpy(usb_out_buffer,"GET_LDR");
ret = usb_bulk_msg(smartlamp_device, usb_sndbulkpipe(smartlamp_device, usb_out), usb_out_buffer, strlen("GET_LDR"), &actual_size, 1000);

if(ret){
continue;
}

ret = usb_bulk_msg(smartlamp_device, usb_rcvbulkpipe(smartlamp_device, usb_in), usb_in_buffer, min(usb_max_size, MAX_RECV_LINE), &actual_size, 1000);
if (ret) {
printk(KERN_ERR "SmartLamp: Erro ao ler dados da USB (tentativa %d). Codigo: %d\n", ret, retries--);
printk("%s", usb_in_buffer);
continue;
}

else{
printk("%s", usb_in_buffer);
int value = 0 ;
int i = 0 ;
for(i = 0; i < 4; i++){
//printk("%d", (int)usb_in_buffer[actual_size -4 + i] -48);
value = (value * 10) + ((int)usb_in_buffer[actual_size -4 + i] -48);
}
return value;
}
//caso tenha recebido a mensagem 'RES_LDR X' via serial acesse o buffer 'usb_in_buffer' e retorne apenas o valor da resposta X
//retorne o valor de X em inteiro
return 0;
}

return -1;
}
}
21 changes: 15 additions & 6 deletions smartlamp-kernel-module/serial_write.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ static uint usb_in, usb_out; // Endereços das portas de e
static char *usb_in_buffer, *usb_out_buffer; // Buffers de entrada e saída da USB
static int usb_max_size; // Tamanho máximo de uma mensagem USB

#define VENDOR_ID SUBSTITUA_PELO_VENDORID /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID SUBSTITUA_PELO_PRODUCTID /* Encontre o ProductID do smartlamp */
#define VENDOR_ID 0x10c4 /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID 0xea60 /* Encontre o ProductID do smartlamp */
static const struct usb_device_id id_table[] = { { USB_DEVICE(VENDOR_ID, PRODUCT_ID) }, {} };

static int usb_probe(struct usb_interface *ifce, const struct usb_device_id *id); // Executado quando o dispositivo é conectado na USB
static void usb_disconnect(struct usb_interface *ifce); // Executado quando o dispositivo USB é desconectado da USB
static int usb_read_serial(void); // Executado para ler a saida da porta serial
static int usb_write_serial(char *cmd, int param);

MODULE_DEVICE_TABLE(usb, id_table);
bool ignore = true;
Expand Down Expand Up @@ -52,7 +53,7 @@ static int usb_probe(struct usb_interface *interface, const struct usb_device_id
usb_out_buffer = kmalloc(usb_max_size, GFP_KERNEL);


usb_write_serial(COMANDO_SMARTLAMP, VALOR);
usb_write_serial("SET_LED", 100);

printk("LDR Value: %d\n", LDR_value);

Expand All @@ -69,11 +70,19 @@ static void usb_disconnect(struct usb_interface *interface) {
static int usb_write_serial(char *cmd, int param) {
int ret, actual_size;
char resp_expected[MAX_RECV_LINE]; // Resposta esperada do comando

char buffer[3];
// use a variavel usb_out_buffer para armazernar o comando em formato de texto que o firmware reconheça
strcpy(usb_out_buffer,cmd);
strcat(usb_out_buffer, " ");
sprintf(buffer,"%d", param);
strcat(usb_out_buffer, buffer);
/* ret = usb_bulk_msg(smartlamp_device, usb_sndbulkpipe(smartlamp_device, usb_out), usb_out_buffer, strlen(usb_out_buffer), &actual_size, 1000);
if(ret){
continue;
}*/

// Grave o valor de usb_out_buffer com printk

printk("%s \n",usb_out_buffer);
// Envie o comando pela porta Serial
ret = usb_bulk_msg(smartlamp_device, usb_sndbulkpipe(smartlamp_device, usb_out), usb_out_buffer, strlen(usb_out_buffer), &actual_size, 1000*HZ);
if (ret) {
Expand All @@ -86,4 +95,4 @@ static int usb_write_serial(char *cmd, int param) {
sprintf(resp_expected, "RES %s", cmd);

return -1;
}
}
105 changes: 87 additions & 18 deletions smartlamp-kernel-module/smartlamp.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <linux/module.h>
#include <linux/usb.h>
#include <linux/slab.h>
#include <linux/delay.h>

MODULE_AUTHOR("DevTITANS <devtitans@icomp.ufam.edu.br>");
MODULE_DESCRIPTION("Driver de acesso ao SmartLamp (ESP32 com Chip Serial CP2102");
Expand All @@ -16,13 +17,14 @@ static uint usb_in, usb_out; // Endereços das portas de e
static char *usb_in_buffer, *usb_out_buffer; // Buffers de entrada e saída da USB
static int usb_max_size; // Tamanho máximo de uma mensagem USB

#define VENDOR_ID SUBSTITUA_PELO_VENDORID /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID SUBSTITUA_PELO_PRODUCTID /* Encontre o ProductID do smartlamp */
#define VENDOR_ID 0x10c4 /* Encontre o VendorID do smartlamp */
#define PRODUCT_ID 0xea60 /* Encontre o ProductID do smartlamp */
static const struct usb_device_id id_table[] = { { USB_DEVICE(VENDOR_ID, PRODUCT_ID) }, {} };

static int usb_probe(struct usb_interface *ifce, const struct usb_device_id *id); // Executado quando o dispositivo é conectado na USB
static void usb_disconnect(struct usb_interface *ifce); // Executado quando o dispositivo USB é desconectado da USB
static int usb_read_serial(void);
//static int usb_read_serial(void);
static int usb_send_cmd(char *cmd, int param);

// Executado quando o arquivo /sys/kernel/smartlamp/{led, ldr} é lido (e.g., cat /sys/kernel/smartlamp/led)
static ssize_t attr_show(struct kobject *sys_obj, struct kobj_attribute *attr, char *buff);
Expand Down Expand Up @@ -55,7 +57,7 @@ static int usb_probe(struct usb_interface *interface, const struct usb_device_id

printk(KERN_INFO "SmartLamp: Dispositivo conectado ...\n");

// Cria arquivos do /sys/kernel/smartlamp/*
// Cria arquivos do /sys/kernel/smartlamp
sys_obj = kobject_create_and_add("smartlamp", kernel_kobj);
ignore = sysfs_create_group(sys_obj, &attr_group); // AQUI

Expand All @@ -68,9 +70,11 @@ static int usb_probe(struct usb_interface *interface, const struct usb_device_id
usb_in_buffer = kmalloc(usb_max_size, GFP_KERNEL);
usb_out_buffer = kmalloc(usb_max_size, GFP_KERNEL);

LDR_value = usb_read_serial();

printk("LDR Value: %d\n", LDR_value);
// msleep(2000);
// LDR_value = usb_read_serial();
printk("LDR Value: %d\n",usb_send_cmd("GET_LDR", -1));
msleep(2000);
printk("SET Value: %d\n", usb_send_cmd("SET_LED",0));

return 0;
}
Expand Down Expand Up @@ -98,10 +102,16 @@ static int usb_send_cmd(char *cmd, int param) {
printk(KERN_INFO "SmartLamp: Enviando comando: %s\n", cmd);

// preencha o buffer // Caso contrário, é só o comando mesmo

strcpy(usb_out_buffer,cmd);
if(param != -1){
char buffer[3];
strcat(usb_out_buffer, " ");
sprintf(buffer,"%d", param);
strcat(usb_out_buffer, buffer);
}
// Envia o comando (usb_out_buffer) para a USB
// Procure a documentação da função usb_bulk_msg para entender os parâmetros
ret = usb_bulk_msg(smartlamp_device, usb_sndbulkpipe(smartlamp_device, usb_out), BUFFER, ?, &actual_size, 1000);
ret = usb_bulk_msg(smartlamp_device, usb_sndbulkpipe(smartlamp_device, usb_out), usb_out_buffer, strlen(usb_out_buffer), &actual_size, 2000);
if (ret) {
printk(KERN_ERR "SmartLamp: Erro de codigo %d ao enviar comando!\n", ret);
return -1;
Expand All @@ -112,12 +122,48 @@ static int usb_send_cmd(char *cmd, int param) {
// Espera pela resposta correta do dispositivo (desiste depois de várias tentativas)
while (retries > 0) {
// Lê dados da USB
ret = usb_bulk_msg(smartlamp_device, usb_rcvbulkpipe(smartlamp_device, usb_in), usb_in_buffer, min(usb_max_size, MAX_RECV_LINE), &actual_size, 1000);
ret = usb_bulk_msg(smartlamp_device, usb_rcvbulkpipe(smartlamp_device, usb_in), usb_in_buffer, min(usb_max_size, MAX_RECV_LINE), &actual_size, 2000);
if (ret) {
printk(KERN_ERR "SmartLamp: Erro ao ler dados da USB (tentativa %d). Codigo: %d\n", ret, retries--);
continue;
}


//int value[5] ;
int value = 0;
int value1 = 0 ;
// char *str_value = kmalloc(usb_max_size, GFP_KERNEL); ;
int i = strlen(usb_in_buffer) ;
int j = i;
// char j = ' ';
// int k = 0;
//printk("%s", usb_in_buffer);
if(strcmp(usb_in_buffer,resp_expected) > 0){
//printk("AQUI");
printk("%s", usb_in_buffer);
//for(i = 0; i < strlen(usb_in_buffer); i++){
//while(strcmp(usb_in_buffer[i]," ") != 0){
while(usb_in_buffer[i] != ' '){
//printk("%d", (int)usb_in_buffer[actual_size -4 + i] -48);
//value = ((int)usb_in_buffer[i-2] - 48);
//strcat(str_value,usb_in_buffer[i-2]);
///value = usb_in_buffer[i-2];
//strcat(str_value, &usb_in_buffer[i-2]);
//printk("%c",value);
i--;
}
i++;
while(i < (j-1)){
//printk("%d",usb_in_buffer[i] - 48);
if(((int)usb_in_buffer[i] -48)>=0){
value = (value*10) + ((int)usb_in_buffer[i] -48);
}
i++;
}
//printk("%d",value);
return value;
}

// adicione a sua implementação do médodo usb_read_serial
}
return -1; // Não recebi a resposta esperada do dispositivo
Expand All @@ -126,14 +172,33 @@ static int usb_send_cmd(char *cmd, int param) {
// Executado quando o arquivo /sys/kernel/smartlamp/{led, ldr} é lido (e.g., cat /sys/kernel/smartlamp/led)
static ssize_t attr_show(struct kobject *sys_obj, struct kobj_attribute *attr, char *buff) {
// value representa o valor do led ou ldr
int value;
//int value;
// attr_name representa o nome do arquivo que está sendo lido (ldr ou led)
const char *attr_name = attr->attr.name;
//const char *attr_name = attr->attr.name;

int value = 0; // variável usada para armazenar o valor lido do dispositivo
const char *attr_name = attr->attr.name; // obtém o nome do atributo que está sendo lido do objeto attr
int ret; // usada para armazenar o resultado das chamadas da funcao usb_send_cmd

// printk indicando qual arquivo está sendo lido
printk(KERN_INFO "SmartLamp: Lendo %s ...\n", attr_name);
printk(KERN_INFO "SmartLamp: Lendo %s ...\n", attr_name);

// Implemente a leitura do valor do led ou ldr usando a função usb_send_cmd()
if (strcmp(attr_name, "led") == 0) {
// Leitura do LED
ret = usb_send_cmd("GET_LED", -1); // chama a função usb_send_cmd para enviar o comando GET_LED
} else if (strcmp(attr_name, "ldr") == 0) {
// Leitura do LDR
ret = usb_send_cmd("GET_LDR", -1); // chama a função usb_send_cmd para enviar o comando GET_LDR
} else {
printk(KERN_ALERT "SmartLamp: Atributo desconhecido %s.\n", attr_name);
return -ENODEV;
}

if (ret < 0) {
printk(KERN_ALERT "SmartLamp: Erro ao ler %s.\n", attr_name); // Verifica se houve um erro ao enviar o comando USB (retornado por usb_send_cmd
return ret;
}

sprintf(buff, "%d\n", value); // Cria a mensagem com o valor do led, ldr
return strlen(buff);
Expand All @@ -152,15 +217,19 @@ static ssize_t attr_store(struct kobject *sys_obj, struct kobj_attribute *attr,
printk(KERN_ALERT "SmartLamp: valor de %s invalido.\n", attr_name);
return -EACCES;
}

printk(KERN_INFO "SmartLamp: Setando %s para %ld ...\n", attr_name, value);

// utilize a função usb_send_cmd para enviar o comando SET_LED X
// utilize a função usb_send_cmd para enviar o comando SET_LED
if (strcmp(attr_name, "ldr") == 0) {
printk(KERN_ALERT "SmartLamp: escrita no arquivo LDR não permitida.\n");
return -EACCES; // Retorna erro ao tentar escrever no arquivo LDR
} else if (strcmp(attr_name, "led") == 0) {
printk(KERN_INFO "SmartLamp: Setando %s para %ld ...\n", attr_name, value);
usb_send_cmd("SET_LED ", value);
}

if (ret < 0) {
printk(KERN_ALERT "SmartLamp: erro ao setar o valor do %s.\n", attr_name);
return -EACCES;
}

return strlen(buff);
}
}
Loading