Contiki 3.x
cfs-cat.c
Go to the documentation of this file.
1 /*
2  * list the number of files in th ecoffee file system
3  */
4 
5 /**
6  * \file
7  * Basic test for CFS/Coffee.
8  * \author
9  * Kirk Martinez, University of Southampton
10  * Philip Basford, pjb@ecs.soton.ac.uk
11  */
12 
13 #include "contiki.h"
14 #include "cfs/cfs.h"
15 #include "cfs/cfs-coffee.h"
16 #include "lib/crc16.h"
17 #include "lib/random.h"
18 
19 #include <stdio.h>
20 #include <string.h>
21 
22 #define SPI_LOCKING
23 
24 #ifdef SPI_LOCKING
25  #include "dev/cc1120.h"
26  #include "dev/cc1120-arch.h"
27 #endif
28 #define DATA_BUFFER_LENGTH 200
29 
30 PROCESS(listcoffee_process, "CFS/Coffee dir list process");
31 AUTOSTART_PROCESSES(&listcoffee_process);
32 
33 
34 uint8_t
35 load_file(char *data_buffer, char *filename)
36 {
37  int fd;
38  uint8_t data_length;
39 #ifdef SPI_LOCKING
40  NETSTACK_MAC.off(0);
41  cc1120_arch_interrupt_disable();
42  CC1120_LOCK_SPI();
43 #endif
44  fd = cfs_open(filename, CFS_READ);
45  if(fd >= 0){
46  data_length = cfs_read(fd, data_buffer, DATA_BUFFER_LENGTH);
47  cfs_close(fd);
48  }else{
49  data_length = 0;
50  }
51 #ifdef SPI_LOCKING
52  CC1120_RELEASE_SPI();
53  cc1120_arch_interrupt_enable();
54  NETSTACK_MAC.on();
55 #endif
56  return data_length;
57 }
58 
59 
60 
61 /* returns the number of files in the directory on Flash
62 */
63 int coffee_cat()
64 {
65 struct cfs_dir dir;
66 struct cfs_dirent dirent;
67  char data_buffer[DATA_BUFFER_LENGTH];
68  int i = 0;
69  uint16_t c;
70  uint16_t length;
71 if(cfs_opendir(&dir, "/") == 0) {
72  while(cfs_readdir(&dir, &dirent) != -1) {
73  length = load_file(data_buffer, dirent.name);
74  i = 0;
75  c = 0;
76  printf("%s %d ", dirent.name, length);
77  while(i < length){
78  printf("%02x", (uint8_t)data_buffer[i]);
79  i = i + 1;
80  }
81  printf("\n");
82  }
83  //cfs_closedir(&dir);
84  }
85 else {
86  printf("opendir failed\n");
87  }
88 
89 return(0);
90 }
91 
92 /*---------------------------------------------------------------------------*/
93 PROCESS_THREAD(listcoffee_process, ev, data)
94 {
95  PROCESS_BEGIN();
96 
97  coffee_cat();
98  PROCESS_END();
99 }
100 /*---------------------------------------------------------------------------*/
int cfs_open(const char *name, int flags)
Open a file.
Definition: cfs-coffee.c:1011
int cfs_opendir(struct cfs_dir *dir, const char *name)
Open a directory for reading directory entries.
Definition: cfs-coffee.c:1283
void cfs_close(int fd)
Close an open file.
Definition: cfs-coffee.c:1047
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define CFS_READ
Specify that cfs_open() should open a file for reading.
Definition: cfs.h:90
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition: process.h:273
int cfs_readdir(struct cfs_dir *dir, struct cfs_dirent *record)
Read a directory entry.
Definition: cfs-coffee.c:1294
Header for the Coffee file system.
CFS header file.
Header file for the CRC16 calculcation
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120