blob: 52844d395f9e479c8dd6e2b8cd82e7228be13c95 [file] [log] [blame]
oliverschmidt88d30bd2005-02-15 15:05:33 +00001/*
2 * Copyright (c) 2004, Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the Institute nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * This file is part of the Contiki operating system.
30 *
31 * Author: Adam Dunkels <adam@sics.se>
32 *
oliverschmidtbab003f2005-05-08 10:40:32 +000033 * $Id: cfs-win32.c,v 1.4 2005/05/08 10:40:32 oliverschmidt Exp $
oliverschmidt88d30bd2005-02-15 15:05:33 +000034 */
35#include "contiki.h"
36
37#include "cfs.h"
38#include "cfs-service.h"
39
40#define WIN32_LEAN_AND_MEAN
41#include <windows.h>
42#include <stdlib.h>
43#include <stdio.h>
44#include <io.h>
45#include <fcntl.h>
46
47static int s_open(const char *n, int f);
48static void s_close(int f);
49static int s_read(int f, char *b, unsigned int l);
50static int s_write(int f, char *b, unsigned int l);
51static int s_opendir(struct cfs_dir *p, const char *n);
52static int s_readdir(struct cfs_dir *p, struct cfs_dirent *e);
53static int s_closedir(struct cfs_dir *p);
54
55static const struct cfs_service_interface interface =
56 {
57 CFS_SERVICE_VERSION,
58 s_open,
59 s_close,
60 s_read,
61 s_write,
62 s_opendir,
63 s_readdir,
64 s_closedir
65 };
66
67EK_EVENTHANDLER(eventhandler, ev, data);
68EK_PROCESS(proc, CFS_SERVICE_NAME, EK_PRIO_NORMAL,
69 eventhandler, NULL, (void *)&interface);
70
71struct cfs_win32_dir {
72 HANDLE handle;
73 unsigned char* name;
74 unsigned int size;
75};
76
77/*---------------------------------------------------------------------------*/
78EK_PROCESS_INIT(cfs_win32_init, arg)
79{
80 arg_free(arg);
81 ek_service_start(CFS_SERVICE_NAME, &proc);
82}
83/*---------------------------------------------------------------------------*/
84EK_EVENTHANDLER(eventhandler, ev, data)
85{
86 switch(ev) {
87 case EK_EVENT_INIT:
88 break;
89 case EK_EVENT_REQUEST_REPLACE:
90 ek_replace((struct ek_proc *)data, &interface);
91 break;
92 case EK_EVENT_REQUEST_EXIT:
93 ek_exit();
94 break;
95 }
96}
97/*---------------------------------------------------------------------------*/
98static int
99s_open(const char *n, int f)
100{
101 if(f == CFS_READ) {
102 return open(n, O_RDONLY);
103 } else {
oliverschmidte23e63e2005-04-10 19:24:08 +0000104 return open(n, O_CREAT|O_TRUNC|O_RDWR);
oliverschmidt88d30bd2005-02-15 15:05:33 +0000105 }
106}
107/*---------------------------------------------------------------------------*/
108static void
109s_close(int f)
110{
111 close(f);
112}
113/*---------------------------------------------------------------------------*/
114static int
115s_read(int f, char *b, unsigned int l)
116{
117 return read(f, b, l);
118}
119/*---------------------------------------------------------------------------*/
120static int
121s_write(int f, char *b, unsigned int l)
122{
123 return write(f, b, l);
124}
125/*---------------------------------------------------------------------------*/
126static int
127s_opendir(struct cfs_dir *p, const char *n)
128{
129 struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p;
130 WIN32_FIND_DATA data;
131 char dirname[MAX_PATH];
132
133 if(n == NULL) {
134 n = "";
135 }
oliverschmidtbab003f2005-05-08 10:40:32 +0000136 sprintf(dirname, "%s/*", n);
oliverschmidt88d30bd2005-02-15 15:05:33 +0000137
138 dir->handle = FindFirstFile(dirname, &data);
139 if(dir->handle == INVALID_HANDLE_VALUE) {
140 dir->name = NULL;
141 return 1;
142 }
143
144 dir->name = strdup(data.cFileName);
oliverschmidt527dd2a2005-04-22 23:20:07 +0000145 dir->size = ((data.nFileSizeLow + 511) / 512) % 1000;
oliverschmidt88d30bd2005-02-15 15:05:33 +0000146
147 return 0;
148}
149/*---------------------------------------------------------------------------*/
150static int
151s_readdir(struct cfs_dir *p, struct cfs_dirent *e)
152{
153 struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p;
154 WIN32_FIND_DATA data;
155
156 if(dir->name == NULL) {
157 return 1;
158 }
159
160 strncpy(e->name, dir->name, sizeof(e->name));
161 e->size = dir->size;
162
163 free(dir->name);
164 if(FindNextFile(dir->handle, &data) == 0) {
165 dir->name = NULL;
166 return 0;
167 }
168
169 dir->name = strdup(data.cFileName);
oliverschmidt527dd2a2005-04-22 23:20:07 +0000170 dir->size = ((data.nFileSizeLow + 511) / 512) % 1000;
oliverschmidt88d30bd2005-02-15 15:05:33 +0000171
172 return 0;
173}
174/*---------------------------------------------------------------------------*/
175static int
176s_closedir(struct cfs_dir *p)
177{
178 struct cfs_win32_dir *dir = (struct cfs_win32_dir *)p;
179
180 free(dir->name);
181 FindClose(dir->handle);
182
183 return 1;
184}
185/*---------------------------------------------------------------------------*/